Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/dannyvankooten/extemplate

Wrapper package for Go's template/html to allow for easy file-based template inheritance.
https://github.com/dannyvankooten/extemplate

Last synced: about 2 months ago
JSON representation

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

Awesome Lists containing this project

README

        

This repository moved to https://git.sr.ht/~dvko/extemplate on 2022-12-06 :warning:

---

# Extemplate [![GoDoc](http://godoc.org/github.com/dannyvankooten/extemplate?status.svg)](http://godoc.org/github.com/dannyvankooten/extemplate) [![Build Status](https://travis-ci.org/dannyvankooten/extemplate.svg)](https://travis-ci.org/dannyvankooten/extemplate) [![Go Report Card](https://goreportcard.com/badge/github.com/dannyvankooten/extemplate)](https://goreportcard.com/report/github.com/dannyvankooten/extemplate) [![Coverage](https://gocover.io/_badge/github.com/dannyvankooten/extemplate)](https://gocover.io/github.com/dannyvankooten/extemplate)

Extemplate is a small wrapper package around [html/template](https://golang.org/pkg/html/template/) to allow for easy file-based template inheritance.

File: `templates/parent.tmpl`
```text

{{ block "title" }}Default title{{ end }}

{{ block "content" }}Default content{{ end }}

```

File: `templates/child.tmpl`
```text
{{ extends "parent.tmpl" }}
{{ define "title" }}Child title{{ end }}
{{ define "content" }}Hello world!{{ end }}
```

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

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:

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

Will result in the following templates:

```text
admin/index.tmpl
admin/edit.tmpl
index.tmpl
```

Check out the [tests](https://github.com/dannyvankooten/extemplate/blob/master/template_test.go) and [examples directory](https://github.com/dannyvankooten/extemplate/tree/master/examples) 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