Wrapper package for Go's template/html to allow for easy file-based template inheritance.

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

<html>
<head>
	<title>{{ block "title" }}Default title{{ end }}</title>
</head>
<body>
	{{ block "content" }}Default content{{ end }} 
</body>
</html>

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:

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

Will result in the following templates:

admin/index.tmpl
admin/edit.tmpl
index.tmpl

Check out the tests and examples directory for more examples.

Benchmarks

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

License

MIT

Owner
Danny van Kooten
Independent developer, writing open-source code for a living.
Danny van Kooten
Similar Resources

Templating system for HTML and other text documents - go implementation

FAQ What is Kasia.go? Kasia.go is a Go implementation of the Kasia templating system. Kasia is primarily designed for HTML, but you can use it for any

Mar 15, 2022

Safe HTML for Go

Safe HTML for Go safehtml provides immutable string-like types that wrap web types such as HTML, JavaScript and CSS. These wrappers are safe by constr

Dec 28, 2022

A strongly typed HTML templating language that compiles to Go code, and has great developer tooling.

A strongly typed HTML templating language that compiles to Go code, and has great developer tooling.

A language, command line tool and set of IDE extensions that makes it easier to write HTML user interfaces and websites using Go.

Dec 29, 2022

mold your templated to HTML/ TEXT/ PDF easily.

mold your templated to HTML/ TEXT/ PDF easily.

mold mold your templated to HTML/ TEXT/ PDF easily. install go get github.com/mayur-tolexo/mold Example 1 //Todo model type Todo struct { Title stri

Jun 7, 2019

Paste basic or template-based keys and passwords

pasteword Paste basic or template-based keys and passwords. Retrieve passwords and temporarily put them into the clipboard for easy copy-pasting. On m

Dec 11, 2021

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

Dec 30, 2022

A handy, fast and powerful go template engine.

A handy, fast and powerful go template engine.

Hero Hero is a handy, fast and powerful go template engine, which pre-compiles the html templates to go code. It has been used in production environme

Dec 27, 2022

Jet template engine

Jet Template Engine for Go Jet is a template engine developed to be easy to use, powerful, dynamic, yet secure and very fast. simple and familiar synt

Jan 4, 2023

A complete Liquid template engine in Go

A complete Liquid template engine in Go

Liquid Template Parser liquid is a pure Go implementation of Shopify Liquid templates. It was developed for use in the Gojekyll port of the Jekyll sta

Dec 15, 2022
Comments
  • Fail golang test

    Fail golang test

    go version go1.14.6 windows/amd64 $ go test --- FAIL: TestLookup (0.00s) template_test.go:32: Lookup: expected template, got nil --- FAIL: TestExecuteTemplate (0.00s) template_test.go:41: ExecuteTemplate: extemplate: no template "child.tmpl" --- FAIL: TestTemplates (0.00s) template_test.go:61: template not found in set: grand-child.tmpl panic: runtime error: invalid memory address or nil pointer dereference [recovered] panic: runtime error: invalid memory address or nil pointer dereference [signal 0xc0000005 code=0x0 addr=0x20 pc=0x58fe92]

    goroutine 12 [running]: testing.tRunner.func1.1(0x5c95c0, 0x786810) C:/programing/sdk/go/src/testing/testing.go:988 +0x314 testing.tRunner.func1(0xc00010ea20) C:/programing/sdk/go/src/testing/testing.go:991 +0x400 panic(0x5c95c0, 0x786810) C:/programing/sdk/go/src/runtime/panic.go:969 +0x174 html/template.(*Template).escape(0x0, 0x0, 0x0) C:/programing/sdk/go/src/html/template/template.go:95 +0x42 html/template.(*Template).Execute(0x0, 0x643720, 0xc00005f9e0, 0x0, 0x0, 0x1, 0x502466) C:/programing/sdk/go/src/html/template/template.go:119 +0x36 github.com/dannyvankooten/extemplate.TestTemplates(0xc00010ea20) C:/Users/kolo/go/src/github.com/dannyvankooten/extemplate/template_test.go:65 +0x2d1 testing.tRunner(0xc00010ea20, 0x60f9f0) C:/programing/sdk/go/src/testing/testing.go:1039 +0xe3 created by testing.(*T).Run C:/programing/sdk/go/src/testing/testing.go:1090 +0x379 exit status 2 FAIL github.com/dannyvankooten/extemplate 0.164s

  • Add support for template reloading and bundling

    Add support for template reloading and bundling

    I finally got back to adding some tests and PRing the enhancements I described in #3.

    Apologies for the length of the PR... it definitely ended up more LOC than I'd expected. Thanks for taking a look!

    Fixes #1, #3

  • Would you like some bindata & template reloading mods?

    Would you like some bindata & template reloading mods?

    Hi. Thanks for putting this together! It covered about 80% of what I was planning to build and saved me a bunch of time. The additions I've made:

    • Optional automatic template reparsing on ExecuteTemplate
    • A basic compiled template/bindata scheme
    • A little bit of (backwards compatible) ExecuteTemplate syntactic sugar so you can:
    // Replace this:
    data := map[string]interface{}{
        "foo": 42,
        "bar": "abc",
    }
    xt.ExecuteTemplate(w, "hello.html", data)
    
    // With this:
    xt.ExecuteTemplate(w, "hello.html", "foo", 42, "bar", "abc")
    

    If you're interested in any of this I'm happy to restructure as PRs to extemplate. No offense if not, as I need the changes for my stuff anyway. You can view the work in progress here:

    https://github.com/kalafut/extemplate/commit/cc4ed1b0c2b50a712eb63047381ef5dae2ecca79

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

quicktemplate A fast, powerful, yet easy to use template engine for Go. Inspired by the Mako templates philosophy. Features Extremely fast. Templates

Dec 26, 2022
Goview is a lightweight, minimalist and idiomatic template library based on golang html/template for building Go web application.

goview Goview is a lightweight, minimalist and idiomatic template library based on golang html/template for building Go web application. Contents Inst

Dec 25, 2022
A template to build dynamic web apps quickly using Go, html/template and javascript
A template to build dynamic web apps quickly using Go, html/template and javascript

gomodest-template A modest template to build dynamic web apps in Go, HTML and sprinkles and spots of javascript. Why ? Build dynamic websites using th

Dec 29, 2022
Package damsel provides html outlining via css-selectors and common template functionality.

Damsel Markup language featuring html outlining via css-selectors, extensible via pkg html/template and others. Library This package expects to exist

Oct 23, 2022
HTML template engine for Go

Ace - HTML template engine for Go Overview Ace is an HTML template engine for Go. This is inspired by Slim and Jade. This is a refinement of Gold. Exa

Jan 4, 2023
Golang Echo and html template.

golang-website-example Golang Echo and html template. move GitHub repository for hello to golang-website-example Visual Studio Code Run and Debug: lau

Feb 4, 2022
Api-go-template - A simple Go API template that uses a controller-service based model to build its routes

api-go-template This is a simple Go API template that uses a controller-service

Feb 18, 2022
Made from template temporalio/money-transfer-project-template-go
Made from template temporalio/money-transfer-project-template-go

Temporal Go Project Template This is a simple project for demonstrating Temporal with the Go SDK. The full 20 minute guide is here: https://docs.tempo

Jan 6, 2022
Go-project-template - Template for a golang project

This is a template repository for golang project Usage Go to github: https://git

Oct 25, 2022
Go-api-template - A rough template to give you a starting point for your API

Golang API Template This is only a rough template to give you a starting point f

Jan 14, 2022