tpl

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Mar 31, 2022 License: MIT Imports: 10 Imported by: 2

README

tpl

build Go Report Card MIT License

Provides helpers on top of html/template to dynamically parse all templates from the specific directory and provides a unified interface for accessing them. In addition, the package provides the ability to use the Jinja2/Django like {{ extends }} tag.

Install and update

go get -u github.com/zero-pkg/tpl

How to use

tmpl := tpl.Must(tpl.New().ParseDir("templates", ".html"))

if err := tmpl.Execute(os.Stdout, "content.html", ""); err != nil {
    panic(err)
}

Explaning extends tag

The {{ extends }} tag is the key here. It tells the package that this template “extends” another template. When the package evaluates this template, it first locates the parent. The extends tag should be the first tag in the template.

Nesting extends
# parent.html
body: {{ block "content" . }}Hi from parent.{{ end }}

# child.html
{{ extends "parent.html" }}
{{ block "content" . }}Hi from child.{{ end }}

# grandchild.html
{{ extends "child.html" }}
{{ block "content" . }}Hi from grandchild.{{ end }}

Rendering grandchild.html will give body: Hi from grandchild.

License

http://www.opensource.org/licenses/mit-license.php

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Templates

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

Templates is a specialized set of templates from "text/template"

func Must

func Must(t *Templates, err error) *Templates

Must is a helper that wraps a call to a function returning (*Templates, error) and panics if the error is non-nil. It is intended for use in variable initializations such as

var t = tpl.Must(tpl.New().ParseDir("html", []string{".html"}))

func New

func New() *Templates

New allocates a new set of templates that can be inited via ParseDir function

func (*Templates) Delims

func (t *Templates) Delims(left, right string) *Templates

Delims sets the action delimiters to the specified strings, to be used in subsequent calls to Parse, ParseFiles, or ParseGlob. 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 (*Templates) Execute

func (t *Templates) Execute(wr io.Writer, name string, data interface{}) error

Execute applies a parsed template to the specified data object, writing the output to wr. If an error occurs executing the template or writing its output, execution stops, but partial results may already have been written to the output writer. A template may be executed safely in parallel, although if parallel executions share a Writer the output may be interleaved.

func (*Templates) Funcs

func (t *Templates) Funcs(funcMap template.FuncMap) *Templates

Funcs adds the elements of the argument map to the template's function map. It must be called before the template is parsed. It panics if a value in the map is not a function with appropriate return type. However, it is legal to overwrite elements of the map. The return value is the template, so calls can be chained.

func (*Templates) Lookup

func (t *Templates) Lookup(name string) *template.Template

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

func (*Templates) ParseDir

func (t *Templates) ParseDir(dir string, exts ...string) (*Templates, error)

ParseDir parses all files from specified directory that names end with specified extensions. Templates may have been extended if they contain extends tag on the first line.

Jump to

Keyboard shortcuts

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