extemplate

package module
v0.0.0-...-1ea33f1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 4, 2020 License: MIT Imports: 9 Imported by: 0

README

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

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Extemplate

type Extemplate struct {
	// contains filtered or unexported fields
}

Extemplate holds a reference to all templates and shared configuration like Delims or FuncMap

func New

func New() *Extemplate

New allocates a new, empty, template map

func (*Extemplate) Add

func (x *Extemplate) Add(name string, fileContents []byte) error

func (*Extemplate) Delims

func (x *Extemplate) Delims(left, right string) *Extemplate

Delims sets the action delimiters to the specified strings, to be used in subsequent calls to ParseDir. Nested template definitions will inherit the settings. An empty delimiter stands for the corresponding default: {{ or }}. The return value is the template, so calls can be chained.

func (*Extemplate) ExecuteTemplate

func (x *Extemplate) ExecuteTemplate(wr io.Writer, name string, data interface{}) error

ExecuteTemplate applies the template named name to the specified data object and writes the output to wr.

func (*Extemplate) Funcs

func (x *Extemplate) Funcs(funcMap template.FuncMap) *Extemplate

Funcs adds the elements of the argument map to the template's function map. It must be called before templates are parsed It panics if a value in the map is not a function with appropriate return type or if the name cannot be used syntactically as a function in a template. It is legal to overwrite elements of the map. The return value is the Extemplate instance, so calls can be chained.

func (*Extemplate) Lookup

func (x *Extemplate) Lookup(name string) *template.Template

Lookup returns the template with the given name It returns nil if there is no such template or the template has no definition.

func (*Extemplate) Parse

func (x *Extemplate) Parse() error

ParseDir walks the given directory root and parses all files with any of the registered extensions. Default extensions are .html and .tmpl If a template file has {{/* extends "other-file.tmpl" */}} as its first line it will parse that file for base templates. Parsed templates are named relative to the given root directory

func (*Extemplate) ParseDir

func (x *Extemplate) ParseDir(root string, extensions []string) error

ParseDir walks the given directory root and parses all files with any of the registered extensions. Default extensions are .html and .tmpl If a template file has {{/* extends "other-file.tmpl" */}} as its first line it will parse that file for base templates. Parsed templates are named relative to the given root directory

Example
package main

import (
	"html/template"
	"os"
	"strings"

	"github.com/dannyvankooten/extemplate"
)

func main() {
	xt := extemplate.New().Funcs(template.FuncMap{
		"tolower": strings.ToLower,
	})
	_ = xt.ParseDir("examples/", []string{".tmpl"})
	_ = xt.ExecuteTemplate(os.Stdout, "child.tmpl", nil)
}
Output:

Hello from child.tmpl
	Hello from partials/question.tmpl

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL