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 ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Execute ¶
func Execute(template, startTag, endTag string, w io.Writer, m map[string]interface{}) (int64, error)
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
Returns the number of bytes written to w.
This function is optimized for constantly changing templates. Use Template.Execute for frozen templates.
func 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
This function is optimized for constantly changing templates. Use Template.ExecuteString for frozen templates.
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 be safe to call from concurrently running goroutines.
TagFunc must write contents to w and return the number of bytes written.
Example ¶
template := "foo[baz]bar" 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) (int, error) { var nn int for _, x := range bazSlice { n, err := w.Write(x) if err != nil { return nn, err } nn += n } return nn, nil }), } s := ExecuteString(template, "[", "]", m) fmt.Printf("%s", s)
Output: foo123456789bar