• Stars
    star
    791
  • Rank 57,558 (Top 2 %)
  • Language
    Go
  • License
    MIT License
  • Created about 9 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Simple and fast template engine for Go

fasttemplate

Simple and fast template engine for Go.

Fasttemplate performs only a single task - it substitutes template placeholders with user-defined values. At high speed :)

Take a look at quicktemplate if you need fast yet powerful html template engine.

Please note that fasttemplate doesn't do any escaping on template values unlike html/template do. So values must be properly escaped before passing them to fasttemplate.

Fasttemplate is faster than text/template, strings.Replace, strings.Replacer and fmt.Fprintf on placeholders' substitution.

Below are benchmark results comparing fasttemplate performance to text/template, strings.Replace, strings.Replacer and fmt.Fprintf:

$ go test -bench=. -benchmem
PASS
BenchmarkFmtFprintf-4                   	 2000000	       790 ns/op	       0 B/op	       0 allocs/op
BenchmarkStringsReplace-4               	  500000	      3474 ns/op	    2112 B/op	      14 allocs/op
BenchmarkStringsReplacer-4              	  500000	      2657 ns/op	    2256 B/op	      23 allocs/op
BenchmarkTextTemplate-4                 	  500000	      3333 ns/op	     336 B/op	      19 allocs/op
BenchmarkFastTemplateExecuteFunc-4      	 5000000	       349 ns/op	       0 B/op	       0 allocs/op
BenchmarkFastTemplateExecute-4          	 3000000	       383 ns/op	       0 B/op	       0 allocs/op
BenchmarkFastTemplateExecuteFuncString-4	 3000000	       549 ns/op	     144 B/op	       1 allocs/op
BenchmarkFastTemplateExecuteString-4    	 3000000	       572 ns/op	     144 B/op	       1 allocs/op
BenchmarkFastTemplateExecuteTagFunc-4   	 2000000	       743 ns/op	     144 B/op	       3 allocs/op

Docs

See http://godoc.org/github.com/valyala/fasttemplate .

Usage

	template := "http://{{host}}/?q={{query}}&foo={{bar}}{{bar}}"
	t := fasttemplate.New(template, "{{", "}}")
	s := t.ExecuteString(map[string]interface{}{
		"host":  "google.com",
		"query": url.QueryEscape("hello=world"),
		"bar":   "foobar",
	})
	fmt.Printf("%s", s)

	// Output:
	// http://google.com/?q=hello%3Dworld&foo=foobarfoobar

Advanced usage

	template := "Hello, [user]! You won [prize]!!! [foobar]"
	t, err := fasttemplate.NewTemplate(template, "[", "]")
	if err != nil {
		log.Fatalf("unexpected error when parsing template: %s", err)
	}
	s := t.ExecuteFuncString(func(w io.Writer, tag string) (int, error) {
		switch tag {
		case "user":
			return w.Write([]byte("John"))
		case "prize":
			return w.Write([]byte("$100500"))
		default:
			return w.Write([]byte(fmt.Sprintf("[unknown tag %q]", tag)))
		}
	})
	fmt.Printf("%s", s)

	// Output:
	// Hello, John! You won $100500!!! [unknown tag "foobar"]

More Repositories

1

fasthttp

Fast HTTP package for Go. Tuned for high performance. Zero memory allocations in hot paths. Up to 10x faster than net/http
Go
21,773
star
2

quicktemplate

Fast, powerful, yet easy to use template engine for Go. Optimized for speed, zero memory allocations in hot paths. Up to 20x faster than html/template
Go
2,967
star
3

fastjson

Fast JSON parser and validator for Go. No custom structs, no code generation, no reflection
Go
2,104
star
4

bytebufferpool

Anti-memory-waste byte buffer pool
Go
1,082
star
5

gorpc

Simple, fast and scalable golang rpc library for high load
Go
685
star
6

httpteleport

Transfer 10Gbps http traffic over 1Gbps networks :)
Go
455
star
7

gozstd

go wrapper for zstd
C
405
star
8

ybc

Fast in-process BLOB cache with persistence support
C
395
star
9

goloris

Slowloris for nginx DoS. Written in go
Go
352
star
10

fastrand

Fast and scalable pseudorandom generator for Go
Go
192
star
11

tcplisten

Customizable TCP net.Listener for Go
Go
142
star
12

gheap

Fast generalized heap tree algorithms in C++ and C. Provides simultaneous support for D-heap and B-heap.
C++
128
star
13

fastrpc

Building blocks for fast rpc systems
Go
83
star
14

tsvreader

Fast reader for TSV streams
Go
62
star
15

chclient

Fast http client for SELECT queries in clickhouse
Go
46
star
16

histogram

Fast histograms for Go
Go
30
star
17

suggester

Suggester - the heart for full-text auto-complete web services
Python
29
star
18

swift-response

Go response to `swift vs node.js benchmarks` :)
Go
19
star
19

batcher

Go package for grouping items in batches
Go
18
star
20

hpajaxrpc

Lightweight RPC library for high-performance AJAX applications
JavaScript
9
star
21

go-launcher

Launcher for Go services (and other executables) accepting over9000 command-line flags
Go
6
star
22

big_int

Arbitrary precision math implementation
C
6
star
23

multiplexing-rpc

Cross-platform RPC library supporting multiplexed and parallel RPC over a single byte stream
C
6
star
24

simple-critbit

Simple implementation of a crit-bit tree in C
C
5
star
25

fiber-framework

Cross-platform framework for userspace threads aka fibers
C
5
star
26

image-resizer-imagemagick

Go
4
star
27

gobcodec

Bytes-oriented codec on top of gob encoding
Go
4
star
28

geocache

A prototype of O(1) nearest dynamic points' locator API in 3D space
Python
2
star
29

image-resizer

Go
2
star