Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
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.
- Host: GitHub
- URL: https://github.com/dannyvankooten/extemplate
- Owner: dannyvankooten
- License: mit
- Archived: true
- Created: 2018-08-10T20:34:19.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2022-12-06T12:37:35.000Z (about 2 years ago)
- Last Synced: 2024-07-31T20:52:55.341Z (4 months ago)
- Language: Go
- Homepage: https://git.sr.ht/~dvko/extemplate
- Size: 36.1 KB
- Stars: 57
- Watchers: 6
- Forks: 15
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-go - extemplate - Tiny wrapper around html/template to allow for easy file-based template inheritance. (Template Engines / HTTP Clients)
- zero-alloc-awesome-go - extemplate - Tiny wrapper around html/template to allow for easy file-based template inheritance. (Template Engines / HTTP Clients)
- awesome-go - extemplate - Wrapper package for Go's template/html to allow for easy file-based template inheritance. - ★ 5 (Template Engines)
- awesome-go-extra - extemplate - based template inheritance.|50|14|1|2018-08-10T20:34:19Z|2021-06-15T11:58:56Z| (Template Engines / HTTP Clients)
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