template

package module
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: May 18, 2020 License: BSD-3-Clause Imports: 14 Imported by: 0

README

lpar/template

This is a quick wrapper for Go's text/template and html/template, providing a higher level template manager for web applications.

Advantages

  • Globbing of filenames relative to a specified starting directory.
  • Templates are named by their path relative to the starting directory, and extensions are included, so template name clashes are less of a problem. For example, a template might be called layout/header.html rather than just header.
  • Files which can be minified using tdewolff/minify are preminified automatically. (This can be disabled.)
  • Group related template files together by naming the template set when you .Load them.
  • Standard familiar html/template style template language.
  • Nested templates, partials and other standard template features still work.
  • A Reload() method provides an easy way to trigger a reload when doing development.
  • Alternatively, you can set .Live = true on the renderer, and each time a template is executed from a template set, the corresponding files will be reloaded into that set.

Example usage

rdr := template.NewRenderer("/srv/app/templates")
err := rdr.Load("usermgr", "users.html", "layout/*.html")
...
err := rdr.Execute("usermgr", os.Stdout, "users.html", data)

Limitations

The Reload() method is unsafe for production use. The standard template libraries don't allow loading of a template after any associated template has been executed. Therefore, Reload() has to throw away all loaded templates and recompile them from scratch. This means that template execution in other goroutines may fail until the appropriate template file has been reloaded. (The code shouldn't crash, though.)

Future features

I'm thinking I should add a way to declare that individual files shouldn't be preminimized, in case I ever encounter a situation where that breaks a template. Maybe putting .min. in the filename to say the file should be considered minimized already?

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Renderer

type Renderer struct {

	// Minify is true if the template content should be minified. It's possible that the minification code could have
	// problems with complex Go templates, but I've not encountered any problems of that nature so far.
	Minify bool
	// Live is true if the template sets should be reloaded whenever Execute is used.
	// This is obviously a very bad idea in production and should only be used for development.
	Live bool
	// contains filtered or unexported fields
}

Renderer is an object for loading, parsing, minifying and rendering HTML templates.

func NewRenderer

func NewRenderer(basepath string) Renderer

NewRenderer returns an initialized Renderer. The basepath is used to locate all files subsequently added via Load().

func (Renderer) Execute added in v0.2.0

func (tm Renderer) Execute(templateSet string, wr io.Writer, tmplname string, data interface{}) error

Execute executes the named template from the named template set.

func (*Renderer) Funcs added in v0.2.1

func (tm *Renderer) Funcs(fm htmlplate.FuncMap)

Funcs sets the function map to be used for all subsequent template load operations.

func (*Renderer) Load added in v0.2.0

func (tm *Renderer) Load(templateSet string, fileglobs ...string) error

Load loads one or more template files into the specified template set.

func (Renderer) Reload

func (tm Renderer) Reload(templateSet string) error

Reload reloads the named template set.

type TemplateSet added in v0.2.0

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

TemplateSet represents a set of 1 or more Go templates.

func (*TemplateSet) Load added in v0.2.0

func (th *TemplateSet) Load() error

Load causes a template set to be loaded from the files in the filesystem. Minification is performed if configured, and the template files are compiled.

Jump to

Keyboard shortcuts

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