Documentation ¶
Index ¶
- Variables
- func CreateHTMLTemplate(name string) *html.Template
- func CreateTemplate(name string) *template.Template
- func ParseFS(t *template.Template, f fs.FS, patterns ...string) error
- func ParseHTMLFS(t *html.Template, f fs.FS, patterns []string, baseDir string) error
- func RenderHtmlTemplateString(tmpl string, data interface{}) (string, error)
- func RenderTemplate(tmpl TemplateExecute, data interface{}) (string, error)
- func RenderTemplateFile(filename string, data interface{}) (string, error)
- func RenderTemplateString(tmpl string, data interface{}) (string, error)
- type TemplateExecute
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var TemplateFuncs = template.FuncMap{ "trim": strings.TrimSpace, "trimRightSpace": trimRightSpace, "trimTrailingWhitespaces": trimRightSpace, "rpad": rpad, "quote": quote, "stripNewlines": stripNewlines, "quoteNewlines": quoteNewlines, "toUpper": strings.ToUpper, "toLower": strings.ToLower, "replaceRegexp": replaceRegexp, "add": add, "sub": sub, "div": div, "mul": mul, "parseFloat": parseFloat, "parseInt": parseInt, "currency": currency, "padLeft": padLeft, "padRight": padRight, "padCenter": padCenter, "bold": bold, "underline": underline, "italic": italic, "strikethrough": strikethrough, "code": code, "codeBlock": codeBlock, "toDate": toDate, "toYaml": toYaml, "indentBlock": indentBlock, "toUrlParameter": toUrlParameter, "styleBold": styleBold, }
TemplateFuncs provides helpers for the standard cobra usage and help templates
Functions ¶
func CreateHTMLTemplate ¶
func CreateTemplate ¶
func ParseFS ¶
ParseFS will recursively glob for all the files matching the given patterns, and load them into one big template (with sub-templates).
The globs use bmatcuk/doublestar and support ** notation for recursive globbing.
Example (BasicUsage) ¶
This example shows how to use ParseFS to load templates from a fs.FS.
tmpl := template.New("main") err := ParseFS(tmpl, templates, "test-templates/**/*.tmpl") if err != nil { panic(errors.Wrap(err, "failed to load templates")) } var buf bytes.Buffer err = tmpl.ExecuteTemplate(&buf, "test-templates/inner.tmpl", nil) if err != nil { panic(errors.Wrap(err, "failed to execute template")) } fmt.Println(buf.String()) // OutputTable: // Template content...
Output:
Example (MultiplePatterns) ¶
This example shows how to use ParseFS to load templates from a fs.FS with multiple patterns. This allows for the targeted loading of multiple subdirectories, for example.
tmpl := template.New("main") err := ParseFS(tmpl, templates, "test-templates/partials/**/*.tmpl", "test-templates/layouts/**/*.tmpl") if err != nil { panic(errors.Wrap(err, "failed to load templates")) } var buf bytes.Buffer err = tmpl.ExecuteTemplate(&buf, "test-templates/layouts/main.tmpl", nil) if err != nil { panic(errors.Wrap(err, "failed to execute template")) } fmt.Println(buf.String()) // OutputTable: // Loading from Partial
Output:
func ParseHTMLFS ¶
ParseHTMLFS will recursively glob for all the files matching the given patterns, and load them into one big template (with sub-templates). It is the html.Template equivalent of ParseFS.
The globs use bmatcuk/doublestar and support ** notation for recursive globbing.
NOTE(manuel, 2023-04-18) Interestingly, we have a baseDir parameter here but only one pattern However, the text.template version supports multiple patterns, but has no basedir. Maybe unify?
func RenderTemplate ¶
func RenderTemplate(tmpl TemplateExecute, data interface{}) (string, error)