Documentation
¶
Index ¶
- type Template
- func (t *Template) Execute(m map[string]any) []byte
- func (t *Template) ExecuteFunc(f func(string) []byte) []byte
- func (t *Template) ExecuteString(m map[string]any) string
- func (t *Template) ExecuteWriter(w io.Writer, m map[string]any) error
- func (t *Template) ExecuteWriterFunc(w io.Writer, f func(string) []byte) error
- func (t *Template) WithTagFunc(tag string, tagFunc func(string) []byte)
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Template ¶
type Template struct {
// contains filtered or unexported fields
}
Template is more simple but fast template engine than [text.Template]. Use NewTemplate to instantiate Template. Template supports formatting primitive types with the listed way. fmt.Sprint is used to fallback.
- nil : "<nil>"
- string : v
- []byte : string(v)
- bool : strconv.FormatBool(v)
- int,int8,int16,int32,int64 : strconv.FormatInt(int64(v), 10)
- uint,uint8,uint16,uint32,uint64 : strconv.FormatUint(uint64(v), 10)
- float32 : strconv.FormatFloat(float64(v), 'g', -1, 32)
- float64 : strconv.FormatFloat(float64(v), 'g', -1, 64)
- complex64 : strconv.FormatComplex(complex128(v), 'g', -1, 64)
- complex128 : strconv.FormatComplex(complex128(v), 'g', -1, 128)
- fmt.Stringer : v.String()
- others : fmt.Sprint(v)
Example ¶
package main import ( "fmt" "github.com/aileron-projects/go/ztext" ) func main() { template := ` nil = {{ nil }} string = {{ string }} int = {{ int }} int8 = {{ int8 }} int16 = {{ int16 }} int32 = {{ int32 }} int64 = {{ int64 }} uint = {{ uint }} uint8 = {{ uint8 }} uint16 = {{ uint16 }} uint32 = {{ uint32 }} uint64 = {{ uint64 }} float32 = {{ float32 }} float64 = {{ float64 }} complex64 = {{ complex64 }} complex128 = {{ complex128 }} struct = {{ struct }} ` tpl := ztext.NewTemplate(template, "{{", "}}") val := map[string]any{ "nil": nil, "string": "foo", "int": 123, "int8": int8(123), "int16": int16(123), "int32": int32(123), "int64": int64(123), "uint": uint(123), "uint8": uint8(123), "uint16": uint16(123), "uint32": uint32(123), "uint64": uint64(123), "float32": float32(1.141592653589), "float64": float64(1.141592653589), "complex64": complex64(123 + 456i), "complex128": complex128(123 + 456i), "struct": struct{ a, b string }{"foo", "bar"}, } fmt.Println(string(tpl.Execute(val))) }
Output: nil = <nil> string = foo int = 123 int8 = 123 int16 = 123 int32 = 123 int64 = 123 uint = 123 uint8 = 123 uint16 = 123 uint32 = 123 uint64 = 123 float32 = 1.1415926 float64 = 1.141592653589 complex64 = (123+456i) complex128 = (123+456i) struct = {foo bar}
func NewTemplate ¶
Template returns a new instance of the Template. Allowed tag name pattern is `[0-9a-zA-Z_\-\.]+`.
func (*Template) Execute ¶
Execute executes template and return resulting []byte. See the comment on Template for supported data types.
func (*Template) ExecuteFunc ¶
ExecuteFunc executes the template with given tag function.
func (*Template) ExecuteString ¶
ExecuteString executes template and returns resulting string. See the comment on Template for supported data types.
func (*Template) ExecuteWriter ¶
ExecuteWriter executes template and writes results into the given writer. [w.Write] will be called multiple times. See the comment on Template for supported data types.
func (*Template) ExecuteWriterFunc ¶
ExecuteWriterFunc executes the template by evaluating tags with given tag function and write results into the given writer. Note that the registered tag func is prior to the given function. io.Write of the given w will be called multiple times.
func (*Template) WithTagFunc ¶
WithTagFunc registers a tag function that returns a value corresponding to tags. Registered tag function is used for evaluating tag values. The given tagFunc replaces existing tagFunc if there already were tagFunc which has the same tag value. WithTagFunc ignores the given tagFunc if the given argument tag is empty or the tagFunc is nil itself. WithTagFunc is not safe for concurrent call.