Documentation
¶
Overview ¶
Package templatecheck checks Go templates for problems. It can detect many errors that are normally caught only during execution. By using templatecheck just after parsing, a program can find errors early in its lifetime, and along template execution paths that might only rarely be reached.
To use templatecheck on a template, that template must be invoked with the same Go type each time. Passing that type to templatecheck gives it enough information to verify that all the field references in the template are valid. templatecheck can also verify that functions are called with the right number of types of arguments, that the argument to a range statement can actually be ranged over, and a few other things.
Consider a web server that parses a template for its home page:
import "html/template"
var tmpl = template.Must(template.New("").ParseFiles("index.tmpl"))
func main() {
...
}
type homePage struct { ... }
func handler(w http.ResponseWriter, r *http.Request) {
var buf bytes.Buffer
if err := tmpl.Execute(&buf, homePage{...}); err != nil {
http.Error(w, ...)
return
}
_, err := w.Write(buf.Bytes())
...
}
Use templatecheck to catch errors at startup, instead of during serving:
func init() {
if err := templatecheck.CheckHTML(tmpl, homePage{}); err != nil {
log.Fatal(err)
}
}
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CheckSafe ¶
CheckSafe checks a github.com/google/safehtml/template for problems. See CheckText for details.
Types ¶
This section is empty.