package ztpl
import "zgo.at/ztpl"
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, funcs ...map[string]interface{}, ) (map[string]*parse.Tree, error)
- func PrintTree(w io.Writer, node parse.Node)
- func Reload(path string) error
- func TestTemplateExecution(m interface{ Run() int }, ignore ...string) int
- func Trace(on bool) internal.Trace
- func Visit(node parse.Node, f func(parse.Node, int) bool)
Functions ¶
func Execute ¶
Execute a named template.
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 IsLoaded ¶
func IsLoaded() bool
IsLoaded reports if templates have been loaded.
func List ¶
func List() []string
List all template names.
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 PrintTree ¶
PrintTree prints the tree to w.
func Reload ¶
Reload the templates from the filesystem.
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.
func Visit ¶
Visit every node and call f.
Traverse in the node if the return value of f is true.
Source Files ¶
visit.go ztpl.go
Directories ¶
Path | Synopsis |
---|---|
internal | |
tplfunc |
- Version
- v0.0.0-20240522225602-574aca1079e7 (latest)
- Published
- May 22, 2024
- Platform
- linux/amd64
- Imports
- 15 packages
- Last checked
- 2 days ago –
Tools for package owners.