Documentation ¶
Overview ¶
Package fasttemplate implements simple and fast template library.
Fasttemplate is faster than text/template, strings.Replace and strings.Replacer.
Fasttemplate ideally fits for fast and simple placeholders' substitutions.
Index ¶
- type TagFunc
- type Template
- func (t *Template) Execute(w io.Writer, m map[string]interface{}) error
- func (t *Template) ExecuteBytes(m map[string]interface{}) []byte
- func (t *Template) ExecuteFunc(w io.Writer, f TagFunc) error
- func (t *Template) ExecuteFuncBytes(f TagFunc) []byte
- func (t *Template) ExecuteFuncString(f TagFunc) string
- func (t *Template) ExecuteString(m map[string]interface{}) string
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type TagFunc ¶
TagFunc can be used as a substitution value in the map passed to Execute*. Execute* functions pass tag (placeholder) name in 'tag' argument.
TagFunc must write contents to w and be safe to call from concurrently running goroutines.
Example ¶
template := "foo[baz]bar" t, err := NewTemplate(template, "[", "]") if err != nil { log.Fatalf("unexpected error when parsing template: %s", err) } bazSlice := [][]byte{[]byte("123"), []byte("456"), []byte("789")} m := map[string]interface{}{ // Always wrap the function into TagFunc. // // "baz" tag function writes bazSlice contents into w. "baz": TagFunc(func(w io.Writer, tag string) error { for _, x := range bazSlice { if _, err := w.Write(x); err != nil { return err } } return nil }), } s := t.ExecuteString(m) fmt.Printf("%s", s)
Output: foo123456789bar
type Template ¶
type Template struct {
// contains filtered or unexported fields
}
Template implements simple template engine, which can be used for fast tags' (aka placeholders) substitution.
Example ¶
template := "https://{{host}}/?foo={{bar}}{{bar}}&q={{query}}&baz={{baz}}" t := New(template, "{{", "}}") // Substitution map. // Since "baz" tag is missing in the map, it will be substituted // by an empty string. m := map[string]interface{}{ "host": "google.com", // string - convenient "bar": []byte("foobar"), // byte slice - the fastest // TagFunc - flexible value. TagFunc is called only if the given // tag exists in the template. "query": TagFunc(func(w io.Writer, tag string) error { _, err := io.WriteString(w, url.QueryEscape(tag+"=world")) return err }), } s := t.ExecuteString(m) fmt.Printf("%s", s)
Output: https://google.com/?foo=foobarfoobar&q=query%3Dworld&baz=
func New ¶
New parses the given template using the given startTag and endTag as tag start and tag end.
The returned template can be executed by concurrently running goroutines using Execute* methods.
New panics if the given template cannot be parsed. Use NewTemplate instead if template may contain errors.
func NewTemplate ¶
NewTemplate parses the given template using the given startTag and endTag as tag start and tag end.
The returned template can be executed by concurrently running goroutines using Execute* methods.
func (*Template) Execute ¶
Execute substitutes template tags (placeholders) with the corresponding values from the map m and writes the result to the given writer w.
Substitution map m may contain values with the following types:
- []byte - the fastest value type
- string - convenient value type
- TagFunc - flexible value type
func (*Template) ExecuteBytes ¶
ExecuteBytes substitutes template tags (placeholders) with the corresponding values from the map m and returns the result.
Substitution map m may contain values with the following types:
- []byte - the fastest value type
- string - convenient value type
- TagFunc - flexible value type
func (*Template) ExecuteFunc ¶
ExecuteFunc calls f on each template tag (placeholder) occurrence.
func (*Template) ExecuteFuncBytes ¶
ExecuteFuncBytes calls f on each template tag (placeholder) occurrence and substitutes it with the data written to TagFunc's w.
Returns the resulting byte slice.
func (*Template) ExecuteFuncString ¶
ExecuteFuncString calls f on each template tag (placeholder) occurrence and substitutes it with the data written to TagFunc's w.
Returns the resulting string.
Example ¶
template := "Hello, [user]! You won [prize]!!! [foobar]" t, err := NewTemplate(template, "[", "]") if err != nil { log.Fatalf("unexpected error when parsing template: %s", err) } s := t.ExecuteFuncString(func(w io.Writer, tag string) error { switch tag { case "user": _, err := io.WriteString(w, "John") return err case "prize": _, err := io.WriteString(w, "$100500") return err default: _, err := fmt.Fprintf(w, "[unknown tag %q]", tag) return err } }) fmt.Printf("%s", s)
Output: Hello, John! You won $100500!!! [unknown tag "foobar"]
func (*Template) ExecuteString ¶
ExecuteString substitutes template tags (placeholders) with the corresponding values from the map m and returns the result.
Substitution map m may contain values with the following types:
- []byte - the fastest value type
- string - convenient value type
- TagFunc - flexible value type