tmplcheck

command module
v0.0.0-...-e3d13b2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 8, 2016 License: MIT Imports: 16 Imported by: 0

README

tmplcheck

wercker status

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.

Documentation

Overview

Tmplcheck performs type checking between templates and go source files.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL