tmplcheck
tmplcheck checks that templates do not use keys not passed in from Go source code in Execute calls.
Currently only supports github.com/go-web-framework/templates
. Future support for html/template
and text/template
is planned. See TODOs in source code.
tmplcheck statically analyzes your source code and templates to help prevent panics at run time such as:
panic: template: test:1:2: executing "test" at <.Cnt>: can't evaluate field Cnt in type main.Inventory
Install
go get -u github.com/go-web-framework/tmplcheck
Quick Start
tmplcheck \
-p <import path of go code> \
-t <path to templates> \
-format <plain|json>
See tmplcheck -help
for more.
Example
tmplcheck
outputs the following for the files below:
root.html
5:13: uses "Title", but hello.go:29: set.Execute is missing "Title"
8:14: uses "X", but hello.go:29: set.Execute is missing "X"
8:17: uses "Y", but hello.go:29: set.Execute is missing "Y"
Template root.html
:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>{{.Title}}</title>
</head>
<body>
{{ if and .X .Y }}
xy
{{ end }}
</body>
</html>
And corresponding Go source code in hello.go
that executes root.html
:
package main
import (
"log"
"os"
"path/filepath"
"github.com/go-web-framework/templates"
)
func main() {
set := &templates.Set{}
err := set.Parse(filepath.Join("..", "templates"))
if err != nil {
log.Fatalln(err)
}
err = set.Execute("root.html", os.Stdout, nil)
if err != nil {
log.Fatalln(err)
}
}
License
MIT. See the LICENSE file at the root of the repo.