4 Simple and fast template engine for Go.
6 Fasttemplate peforms only a single task - it substitutes template placeholders
7 with user-defined values. At high speed :)
9 Take a look at [quicktemplate](https://github.com/valyala/quicktemplate) if you need fast yet powerful html template engine.
11 *Please note that fasttemplate doesn't do any escaping on template values
12 unlike [html/template](http://golang.org/pkg/html/template/) do. So values
13 must be properly escaped before passing them to fasttemplate.*
15 Fasttemplate is faster than [text/template](http://golang.org/pkg/text/template/),
16 [strings.Replace](http://golang.org/pkg/strings/#Replace),
17 [strings.Replacer](http://golang.org/pkg/strings/#Replacer)
18 and [fmt.Fprintf](https://golang.org/pkg/fmt/#Fprintf) on placeholders' substitution.
20 Below are benchmark results comparing fasttemplate performance to text/template,
21 strings.Replace, strings.Replacer and fmt.Fprintf:
24 $ go test -bench=. -benchmem
26 BenchmarkFmtFprintf-4 2000000 790 ns/op 0 B/op 0 allocs/op
27 BenchmarkStringsReplace-4 500000 3474 ns/op 2112 B/op 14 allocs/op
28 BenchmarkStringsReplacer-4 500000 2657 ns/op 2256 B/op 23 allocs/op
29 BenchmarkTextTemplate-4 500000 3333 ns/op 336 B/op 19 allocs/op
30 BenchmarkFastTemplateExecuteFunc-4 5000000 349 ns/op 0 B/op 0 allocs/op
31 BenchmarkFastTemplateExecute-4 3000000 383 ns/op 0 B/op 0 allocs/op
32 BenchmarkFastTemplateExecuteFuncString-4 3000000 549 ns/op 144 B/op 1 allocs/op
33 BenchmarkFastTemplateExecuteString-4 3000000 572 ns/op 144 B/op 1 allocs/op
34 BenchmarkFastTemplateExecuteTagFunc-4 2000000 743 ns/op 144 B/op 3 allocs/op
41 See http://godoc.org/github.com/valyala/fasttemplate .
48 template := "http://{{host}}/?q={{query}}&foo={{bar}}{{bar}}"
49 t := fasttemplate.New(template, "{{", "}}")
50 s := t.ExecuteString(map[string]interface{}{
52 "query": url.QueryEscape("hello=world"),
58 // http://google.com/?q=hello%3Dworld&foo=foobarfoobar
66 template := "Hello, [user]! You won [prize]!!! [foobar]"
67 t, err := fasttemplate.NewTemplate(template, "[", "]")
69 log.Fatalf("unexpected error when parsing template: %s", err)
71 s := t.ExecuteFuncString(func(w io.Writer, tag string) (int, error) {
74 return w.Write([]byte("John"))
76 return w.Write([]byte("$100500"))
78 return w.Write([]byte(fmt.Sprintf("[unknown tag %q]", tag)))
84 // Hello, John! You won $100500!!! [unknown tag "foobar"]