template

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Nov 13, 2018 License: BSD-3-Clause Imports: 16 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

  • It keeps track of whether each file is HTML or not, and uses html/template where appropriate for safety.
  • Pass it a directory and it recurses through and loads and parses all the files. Each template is named with the filename relative to the specified directory.
  • Files which can be minified using tdewolff/minify are preminified automatically.
  • Standard familiar html/template style API.
  • Nested templates, partials and other template features still work.
  • A Reload() method provides an easy way to trigger a reload when doing development.

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 reload 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 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 {
	// contains filtered or unexported fields
}

Renderer is an object which loads, minifies and renders templates for CSS, JS, HTML, SVG, and XML.

func NewRenderer

func NewRenderer() *Renderer

NewRenderer returns a Renderer ready for use.

func (*Renderer) ExecuteTemplate

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

ExecuteTemplate executes the template with the specified name, passing it the provided data, and sending the output to the provided io.Writer.

func (*Renderer) ParseFiles

func (r *Renderer) ParseFiles(basepath string) error

ParseFiles loads, minifies and parses all template files under the supplied base path. Each template is named according to its relative path under the base path. HTML files are parsed into html/template templates, other files are parsed into text/template templates.

func (*Renderer) Reload

func (r *Renderer) Reload() error

Reload causes the Renderer to rescan all of the directories it has been told to scan before, and reload all of the template files.

This method is intended for use during development. It should not be used in production, as the cached template update process is unsafe and template execution in other goroutines may fail while the files are being reloaded.

Jump to

Keyboard shortcuts

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