itpl

package module
v1.1.2 Latest Latest
Warning

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

Go to latest
Published: Sep 24, 2018 License: MIT Imports: 6 Imported by: 0

README

itpl

Package itpl allows to include one Go templates into anothers.

See documentation on GoDoc: https://godoc.org/github.com/davidmz/itpl

Documentation

Overview

Package itpl allows to include one Go templates into anothers.

This package introduce an 'include' action that works like that:

// "page.tpl"
{{include "header.tpl"}}
Content
{{include "footer.tpl"}}

// "header.tpl"
Header

// "footer.tpl"
Footer

itpl.Load("page.tpl") will load "page.tpl" file, include content of "header.tpl" and "footer.tpl" files at the places of 'include' actions and produce the following template code:

Header
Content
Footer

Of course any more complex template logic can be used. The Load function returns a combined template as a string that can be parsed and executed used with text/template of html/template package.

The include action required one string argument, the relative or absolute path to included file. If relative path is used then it will be resolved relative to file with the include action:

// /1.tpl
{{include "inc/2.tpl"}}

// /inc/2.tpl
{{include "3.tpl"}}

// /inc/3.tpl
Some content

This package does not execute templates, it just parses them and combine content of multiple files into one. So the include actions cannot use any dynamic variables or parameters to construct the file path. File path must be a regular quoted string.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Load

func Load(fileName string) (string, error)

Load loads template from OS filesystem and process the include actions. It returns processed template as a string or error if something went wrong.

Types

type Loader

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

Loader allows to load and process templates with more options that the bare Load function. Loader can load files from non-standard filesystem (it uses github.com/spf13/afero as filesystem abstraction).

func NewLoader

func NewLoader() *Loader

NewLoader creates a new Loader based on OS filesystem.

func (*Loader) Fs

func (ld *Loader) Fs(fs afero.Fs) *Loader

Fs allow to change filesystem of Loader. It may be useful in tests with in-memory filesystem.

func (*Loader) Load

func (ld *Loader) Load(fileName string) (string, error)

Load loads template and process the include actions. It returns processed template as a string or error if something went wrong.

Jump to

Keyboard shortcuts

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