plates

package module
v0.3.3 Latest Latest
Warning

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

Go to latest
Published: Oct 20, 2025 License: MIT Imports: 9 Imported by: 2

README

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

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ParserFormat = ParserFunc(FormatParser)
View Source
var ParserHTML = ParserFunc(HTMLParser)
View Source
var ParserText = ParserFunc(TextParser)

Functions

This section is empty.

Types

type Aborted

type Aborted interface {
	Aborted() error
}

Aborted is a special purpose error used to signal an aborted template execution. It prevents saving output files.

type Executor

type Executor interface {
	Execute(w io.Writer, data interface{}) error
}

Executor executes a template against an io.Writer given the context.

type ExecutorFunc

type ExecutorFunc func(w io.Writer, data interface{}) error

ExecutorFunc executes a template against an io.Writer given the context.

func (ExecutorFunc) Execute

func (f ExecutorFunc) Execute(w io.Writer, data interface{}) error

type Factory added in v0.3.0

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

Factory allows fluent execution of templates and runtime selection of the template engine.

func New

func New(name string) *Factory

New creates a properly initialized Factory. Name is an arbitrary label passed to the internal template engines

func (*Factory) AddFuncs added in v0.3.0

func (t *Factory) AddFuncs(in ...FuncMap) *Factory

AddFuncs adds the provided FuncMap to the library

func (*Factory) AddMatcher added in v0.3.0

func (t *Factory) AddMatcher(in ...Matcher) *Factory

func (*Factory) AddMatcherFunc added in v0.3.0

func (t *Factory) AddMatcherFunc(in ...MatcherFunc) *Factory

func (*Factory) Default added in v0.3.0

func (t *Factory) Default(in Parser) *Factory

func (*Factory) DefaultFunc added in v0.3.0

func (t *Factory) DefaultFunc(in ParserFunc) *Factory

func (*Factory) FileReader added in v0.3.0

func (t *Factory) FileReader(in FileReader) *Factory

FileReader allows overriding how filenames are evaluated and loaded.

func (*Factory) FileReaderFunc added in v0.3.0

func (t *Factory) FileReaderFunc(in FileReaderFunc) *Factory

FileReaderFunc allows overriding how filenames are evaluated and loaded.

func (Factory) From added in v0.3.0

func (t Factory) From(s string) Template

func (Factory) FromFile added in v0.3.0

func (t Factory) FromFile(templateFile string) Template

func (Factory) FromName added in v0.3.0

func (t Factory) FromName(name, s string) Template

FromName uses the engine matchers to find the approprite template engine based on the name

type FileReader

type FileReader interface {
	ReadFile(filename string) ([]byte, error)
}

FileReader allows overriding how filenames are evaluated and loaded.

type FileReaderFunc

type FileReaderFunc func(filename string) ([]byte, error)

FileReaderFunc allows overriding how filenames are evaluated and loaded.

func (FileReaderFunc) ReadFile

func (f FileReaderFunc) ReadFile(name string) ([]byte, error)

type FormatEngine added in v0.2.0

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

FormatEngine uses the golang fmt package to process a string. Warning: It is not possible to generate an error for an invalid format string. Go will embed the error message in the output string, for example: `%!d(string=foo)` for an invalid type or `%!s(MISSING)` for missing data

func (FormatEngine) Execute added in v0.2.0

func (f FormatEngine) Execute(w io.Writer, data interface{}) error

type FuncMap

type FuncMap map[string]interface{}

FuncMap maps template function names to functions.

func (FuncMap) Add

func (ff FuncMap) Add(in ...FuncMap)

Add takes the inbound FuncMaps and joins them to the current FuncMap.

type Map added in v0.3.0

type Map struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

Map is a thread-safe cache for templates

func NewMap added in v0.3.0

func NewMap() *Map

func (*Map) Clear added in v0.3.0

func (c *Map) Clear()

Clear resets the map.

func (*Map) Delete added in v0.3.0

func (c *Map) Delete(k string)

Delete an item from the Map. Does nothing if the key is not in the Map.

func (*Map) Get added in v0.3.0

func (c *Map) Get(k string) (Template, bool)

Get an item from the Map. Returns the item or nil, and a bool indicating whether the key was found.

func (*Map) Set added in v0.3.0

func (c *Map) Set(k string, v Template)

type Matcher

type Matcher interface {
	Match(filename string) Parser
}

Matcher is used to match a file name to a Parser.

type MatcherFunc

type MatcherFunc func(string) Parser

MatcherFunc is used to match a file name to a Parser.

func (MatcherFunc) Match

func (f MatcherFunc) Match(filename string) Parser

type Parser

type Parser interface {
	Parse(name string, funcs FuncMap, input string) (Template, error)
}

Parser reads the input template string and returns an executable Template.

func MatchFormat added in v0.2.0

func MatchFormat(filename string) Parser

MatchFormat returns a Go Format Parser if the filename ends with ".format" or ".string" or ".goformat"

func MatchHTML

func MatchHTML(filename string) Parser

MatchHTML returns an HTMLParser if the filename ends with ".htm.tpl" or ".html.tpl"

func MatchText

func MatchText(filename string) Parser

MatchText returns a TextParser if the filename ends with ".txt.tpl"

type ParserFunc

type ParserFunc func(name string, funcs FuncMap, input string) (Template, error)

ParserFunc reads the input template string and returns an executable Template.

func (ParserFunc) Parse

func (f ParserFunc) Parse(name string, funcs FuncMap, input string) (Template, error)

type Template

type Template interface {
	To(data interface{}) (string, error)
	ToFile(file string, data interface{}) error
	ToWriter(w io.Writer, data interface{}) error
}

Template is used to execute the parsed template and write the output to a string, File, or io.Writer.

func FormatParser added in v0.2.0

func FormatParser(name string, funcs FuncMap, in string) (Template, error)

func HTMLParser

func HTMLParser(name string, funcs FuncMap, in string) (Template, error)

func NewEngine

func NewEngine(executor Executor) Template

func TextParser

func TextParser(name string, funcs FuncMap, in string) (Template, error)

Directories

Path Synopsis
plush module

Jump to

Keyboard shortcuts

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