Documentation
¶
Overview ¶
Example ¶
package main
import (
"bytes"
"fmt"
"log"
"github.com/rytsh/mugo/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]any{
"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 Option
- type OptionExecute
- type OptionTemplate
- type SubFuncs
- type Template
- func (t *Template) AddFunc(name string, fn any)
- func (t *Template) AddFuncMap(funcMap map[string]any)
- func (t *Template) Clone() (*Template, error)
- 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) ParseFS(fsys fs.FS, patterns ...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 ¶
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 ¶
type OptionTemplate func(*optionsTemplate)
func WithAddFunc ¶
func WithAddFunc(key string, f any) OptionTemplate
WithAddFunc for adding a function.
func WithAddFuncMap ¶
func WithAddFuncMap(funcMap map[string]any) OptionTemplate
WithAddFuncMap for adding multiple functions.
func WithAddFuncMapWithOpts ¶ added in v0.9.0
func WithAddFuncMapWithOpts(fn func(o Option) map[string]any) OptionTemplate
func WithAddFuncWithOpts ¶ added in v0.9.0
func WithAddFuncWithOpts(fn func(o Option) (string, any)) OptionTemplate
func WithHTMLTemplate ¶
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.8.1
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 ¶
func (t *Template) SetTypeText()
SetTypeText converts the template to text template.
This function will reset the template when switching from html to text template.