Skip to content

Repository files navigation

plates GitHub Workflow Status codecov PkgGoDev Report card Total alerts GitHub release

Plates is a simple abstraction to enable a simple fluent syntax for template execution, and to allow runtime selection of the template engine. Currently, Plates supports the go stdlib HTML and Text Parsers and the Buffalo Plush engine. Note that the Plush support is in the subpackage github.com/gofoji/plate/plush to minimize dependencies. If there are additional engines you would like to see, please reference the plush package to see how easy it is to add.

Why

We needed a simple way for foji to support multiple template engines selected at runtime. After building this support we then needed it for a separate service and decided to abstract the package to let others enjoy the functionality.

Installation

go get -u github.com/gofoji/plates

Usage

Simple Example

out, err := plates.New("").
	DefaultFunc(plates.TextParser).
	From(`{{print "<h1>A header!</h1>"}}`).
	To(nil)
if err != nil {
    return err
}

Output

<h1>A header!</h1>

Advanced Example (from foji)

This shows an example of configuring the wrapper once and then using it to generate and execute multiple template instances.

t := plates.New("").Funcs(runtime.Funcs, sprig.TxtFuncMap()).
	AddMatcherFunc(plates.MatchText, plush.Match).
    DefaultFunc(plates.MatchText)

targetFile, err := t.From(targetFile).To(data)
if err != nil {
    return err
}

err = t.FromFile(templateFile).ToFile(targetFile, data)
if err != nil {
    return err
}

The first call to t.From() uses the default parser (plates.MatchText). The next parse call uses FromFile which uses the filename to determine which template engine to invoke. The selection is based on the order provided and if none of the filenames match it falls back to the Default.

TODO

  • Identify additional template engines
  • Review performance characteristics
  • Godoc examples

About

Simple abstraction to allow runtime selection of the template execution

Resources

Stars

2 stars

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors

Languages