Documentation
¶
Overview ¶
Package ztpl implements the loading and reloading of templates.
Index ¶
- func Execute(w io.Writer, name string, data interface{}) error
- func ExecuteBytes(name string, data interface{}) ([]byte, error)
- func ExecuteString(name string, data interface{}) (string, error)
- func HasTemplate(name string) bool
- func Init(files fs.FS) error
- func IsLoaded() bool
- func List() []string
- func Parse(name, text string, mode parse.Mode, leftDelim, rightDelim string, ...) (map[string]*parse.Tree, error)
- func PrintTree(w io.Writer, node parse.Node)
- func Reload(path string) error
- func TestTemplateExecution(m interface{ ... }, ignore ...string) int
- func Trace(on bool) internal.Trace
- func Visit(node parse.Node, f func(parse.Node, int) bool)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ExecuteBytes ¶
ExecuteBytes a named template and return the data as a byte slice.
func ExecuteString ¶
ExecuteString a named template and return the data as a string.
func HasTemplate ¶
HasTemplate reports if this template is loaded.
func Init ¶
Init sets up the templates.
This assumes that HTML templates have the .gohtml extension, and that text templates have the .gotxt extension.
func Parse ¶
func Parse( name, text string, mode parse.Mode, leftDelim, rightDelim string, funcs ...map[string]interface{}, ) (map[string]*parse.Tree, error)
Parse a template and set the mode.
func TestTemplateExecution ¶
TestTemplateExecution tests if all templates loaded through ztpl are executed.
Go templates are dynamically typed and not counted in code coverage tools; this is a simple way to ensure all templates are executed at least once.
Typical usage would be:
func TestMain(m *testing.M) { os.Exit(ztpl.TestTemplateExecution(m, "ignore_this.gohtml")) }
func TestTemplateExecution(m *testing.M, ignore ...string) int {
func Trace ¶
Trace enables tracking of all template executions. When this is disabled the stored list is emptied and the previous value returned.
This is mostly useful for tracking which templates are run from tests, e.g.:
func TestMain(m *testing.M) { ztpl.Trace(true) c := m.Run() ran := ztpl.Trace(false) for _, t := range ztpl.List() { if _, ok := ran[t]; !ok { fmt.Println("didn't execute template", t) } } os.Exit(c) }
Also see TestTemplateExecution(), which wraps this for convenient use.
Types ¶
This section is empty.