templating

package
v0.5.12 Latest Latest
Warning

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

Go to latest
Published: Apr 4, 2024 License: MIT Imports: 19 Imported by: 9

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
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 CreateHTMLTemplate(name string) *html.Template

func CreateTemplate

func CreateTemplate(name string) *template.Template

func ParseFS

func ParseFS(t *template.Template, f fs.FS, patterns ...string) error

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

func ParseHTMLFS(t *html.Template, f fs.FS, patterns []string, baseDir string) error

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 RenderHtmlTemplateString

func RenderHtmlTemplateString(tmpl string, data interface{}) (string, error)

func RenderTemplate

func RenderTemplate(tmpl TemplateExecute, data interface{}) (string, error)

func RenderTemplateFile

func RenderTemplateFile(filename string, data interface{}) (string, error)

func RenderTemplateString

func RenderTemplateString(tmpl string, data interface{}) (string, error)

Types

type TemplateExecute

type TemplateExecute interface {
	Execute(wr io.Writer, data any) error
}

Jump to

Keyboard shortcuts

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