Wrapper package for Go's template/html to allow for easy file-based template inheritance.
b84ab4d9 — Danny van Kooten 1 year, 4 months ago
fix http link
8f1c15f7 — Danny van Kooten 2 years ago
try forced line-ending through gitattributes to get example output test to pass
e503fa84 — Danny van Kooten 2 years ago
fix newline characters in tests for windows?



You can also use your local clone with git send-email.

#Extemplate GoDoc Build Status Go Report Card Coverage

Extemplate is a small wrapper package around html/template to allow for easy file-based template inheritance.

File: templates/parent.tmpl

	<title>{{ block "title" }}Default title{{ end }}</title>
	{{ block "content" }}Default content{{ end }} 

File: templates/child.tmpl

{{ extends "parent.tmpl" }}
{{ define "title" }}Child title{{ end }}
{{ define "content" }}Hello world!{{ end }}

File: main.go

xt := extemplate.New()
xt.ParseDir("templates/", []string{".tmpl"})
_ = xt.ExecuteTemplate(os.Stdout, "child.tmpl", "no data needed") 
// Output: <html>.... Hello world! ....</html>

Extemplate recursively walks all files in the given directory and will parse the files matching the given extensions as a template. Templates are named by path and filename, relative to the root directory.

For example, calling ParseDir("templates/", []string{".tmpl"}) on the following directory structure:

  |__ admin/
  |      |__ index.tmpl
  |      |__ edit.tmpl
  |__ index.tmpl

Will result in the following templates:


Check out the tests and examples directory for more examples.


You will most likely never have to worry about performance, when using this package properly. The benchmarks are purely listed here so we have a place to keep track of progress.

BenchmarkExtemplateGetLayoutForTemplate-8   	 2000000	       923 ns/op	     104 B/op	       3 allocs/op
BenchmarkExtemplateParseDir-8               	    5000	    227898 ns/op	   34864 B/op	     325 allocs/op