Documentation
¶
Overview ¶
Example ¶
package main import ( "bytes" "fmt" "log" "github.com/rytsh/mugo/pkg/templatex" ) func main() { tpl := templatex.New() tpl.AddFunc("add", func(a, b int) int { return a + b }) tpl.AddFunc("sub", func(a, b int) int { return a - b }) var output bytes.Buffer if err := tpl.Execute( templatex.WithIO(&output), templatex.WithData(map[string]interface{}{ "a": 1, "b": 2, }), templatex.WithContent(`a + b = {{ add .a .b }}`+"\n"+`a - b = {{ sub .a .b }}`), ); err != nil { log.Fatal(err) } fmt.Printf("%s", output.String()) }
Output: a + b = 3 a - b = -1
Index ¶
- Variables
- type Info
- type OptionExecute
- type OptionTemplate
- func WithAddFunc(key string, f interface{}) OptionTemplate
- func WithAddFuncMap(funcMap map[string]interface{}) OptionTemplate
- func WithAddFuncTpl[T any](key string, f func(T) interface{}) OptionTemplate
- func WithAddFuncsTpl[T any](fn func(T) map[string]interface{}) OptionTemplate
- func WithFnValue[T any](fn T) OptionTemplate
- func WithHTMLTemplate() OptionTemplate
- type Template
- func (t *Template) AddFunc(name string, fn interface{})
- func (t *Template) AddFuncMap(funcMap map[string]any)
- func (t *Template) Execute(opts ...OptionExecute) error
- func (t *Template) ExecuteTemplate(wr io.Writer, name string, data any) error
- func (t *Template) FuncInfos() []Info
- func (t *Template) ListFuncs() []Info
- func (t *Template) Parse(content string) error
- func (t *Template) ParseGlob(pattern string) error
- func (t *Template) Reset()
- func (t *Template) SetDelims(left, right string) *Template
- func (t *Template) SetTypeHtml()
- func (t *Template) SetTypeText()
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var DefaultTemplateName = "_templatex"
Functions ¶
This section is empty.
Types ¶
type OptionExecute ¶ added in v0.2.0
type OptionExecute func(options *options)
OptionExecute to execute the template.
func WithContent ¶
func WithContent(content string) OptionExecute
WithContent sets the content to parse, if WithParsed used this option is ignored.
func WithData ¶
func WithData(values any) OptionExecute
WithData sets the data to use in Execute* functions. This is the values passed to the template.
func WithIO ¶
func WithIO(w io.Writer) OptionExecute
WithIO sets the writer to use. Useful for Execute function.
func WithParsed ¶
func WithParsed(parsed bool) OptionExecute
WithParsed sets the parsed template to use in Execute* functions.
func WithTemplate ¶
func WithTemplate(template string) OptionExecute
WithTemplate sets the specific template to execute.
type OptionTemplate ¶ added in v0.2.0
type OptionTemplate func(*optionsTemplate)
func WithAddFunc ¶ added in v0.2.0
func WithAddFunc(key string, f interface{}) OptionTemplate
WithAddFunc for adding a function.
func WithAddFuncMap ¶ added in v0.2.0
func WithAddFuncMap(funcMap map[string]interface{}) OptionTemplate
WithAddFuncMap for adding multiple functions.
func WithAddFuncTpl ¶ added in v0.2.0
func WithAddFuncTpl[T any](key string, f func(T) interface{}) OptionTemplate
WithAddFuncTpl for adding a function. The function is execute with the value passed to WithFnValue.
func WithAddFuncsTpl ¶ added in v0.2.0
func WithAddFuncsTpl[T any](fn func(T) map[string]interface{}) OptionTemplate
WithAddFuncsTpl for adding multiple functions. The function is execute with the value passed to WithFnValue.
func WithFnValue ¶ added in v0.2.0
func WithFnValue[T any](fn T) OptionTemplate
WithFnValue for passing a value to the function for WithAddFuncsTpl and WithAddFuncTpl. In templatex, default value is templatex.
func WithHTMLTemplate ¶ added in v0.2.0
func WithHTMLTemplate() OptionTemplate
type Template ¶
type Template struct {
// contains filtered or unexported fields
}
func (*Template) AddFuncMap ¶
AddFuncMap for extra textTemplate functions.
func (*Template) Execute ¶
func (t *Template) Execute(opts ...OptionExecute) error
Execute the template and write the output to the buffer. Add WithIO to change the writer.
Example to execute a template with data and use parsed template:
t.Execute(WithTemplate(templateName), WithData(data), WithParsed(true))
Example to execute and return the result:
var buf bytes.Buffer t.Execute(WithIO(&buf), WithData(data))
func (*Template) ExecuteTemplate ¶
func (*Template) ParseGlob ¶
ParseGlob parses the template definitions in the files identified by the pattern.
func (*Template) SetDelims ¶
SetDelims sets the template delimiters to the specified strings and returns the template to allow chaining.
func (*Template) SetTypeHtml ¶ added in v0.2.0
func (t *Template) SetTypeHtml()
SetTypeHtml converts the template to html template.
This function will reset the template when switching from text to html template.
func (*Template) SetTypeText ¶ added in v0.2.0
func (t *Template) SetTypeText()
SetTypeText converts the template to text template.
This function will reset the template when switching from html to text template.