1
0
mirror of https://github.com/danog/liquid.git synced 2024-11-27 04:46:31 +01:00
liquid/engine.go
2017-07-01 23:52:38 -04:00

66 lines
1.7 KiB
Go

package liquid
import (
"io"
"github.com/osteele/liquid/chunks"
"github.com/osteele/liquid/filters"
"github.com/osteele/liquid/tags"
)
type engine struct{ settings chunks.Settings }
// NewEngine returns a new template engine.
func NewEngine() Engine {
e := engine{chunks.NewSettings()}
filters.AddStandardFilters(e.settings.ExpressionSettings)
tags.AddStandardTags(e.settings)
return e
}
// DefineStartTag is in the Engine interface.
func (e engine) DefineStartTag(name string, td func(io.Writer, chunks.RenderContext) error) {
e.settings.AddStartTag(name).Renderer(td)
}
// DefineFilter is in the Engine interface.
func (e engine) DefineFilter(name string, fn interface{}) {
e.settings.AddFilter(name, fn)
}
// DefineTag is in the Engine interface.
func (e engine) DefineTag(name string, td TagDefinition) {
// For simplicity, don't expose the two stage parsing/rendering process to clients.
// Client tags do everything at runtime.
e.settings.AddTag(name, func(_ string) (func(io.Writer, chunks.RenderContext) error, error) {
return td, nil
})
}
// ParseTemplate is in the Engine interface.
func (e engine) ParseTemplate(text []byte) (Template, error) {
ast, err := e.settings.Parse(string(text))
if err != nil {
return nil, err
}
return &template{ast, e.settings}, nil
}
// ParseAndRender is in the Engine interface.
func (e engine) ParseAndRender(text []byte, c Context) ([]byte, error) {
t, err := e.ParseTemplate(text)
if err != nil {
return nil, err
}
return t.Render(c)
}
// ParseAndRenderString is in the Engine interface.
func (e engine) ParseAndRenderString(text string, c Context) (string, error) {
b, err := e.ParseAndRender([]byte(text), c)
if err != nil {
return "", err
}
return string(b), nil
}