Skip to content
Wrapper package for Go's template/html to allow for easy file-based template inheritance.
Branch: master
Clone or download
Type Name Latest commit message Commit time
Failed to load latest commit information.
examples read files only once Aug 13, 2018
.travis.yml improve readme & fix travis file location Mar 25, 2017
LICENSE first commit Mar 25, 2017 add gocover badge Aug 17, 2018
example_test.go simplify all examples Aug 13, 2018
template.go don't parse non-child templates on every iteration, but still add to map Aug 18, 2018
template_test.go improve internal api Aug 15, 2018

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



You can’t perform that action at this time.