1
0
mirror of https://github.com/danog/liquid.git synced 2024-11-26 21:34:44 +01:00
liquid/engine.go

99 lines
2.7 KiB
Go
Raw Normal View History

package liquid
import (
2017-06-27 13:43:42 +02:00
"io"
2017-06-27 18:06:24 +02:00
"github.com/osteele/liquid/filters"
2017-07-04 17:03:18 +02:00
"github.com/osteele/liquid/render"
2017-06-30 22:46:17 +02:00
"github.com/osteele/liquid/tags"
)
2017-07-07 11:51:31 +02:00
// An Engine parses template source into renderable text.
//
// An engine can be configured with additional filters and tags.
type Engine struct{ cfg render.Config }
2017-07-07 11:51:31 +02:00
// NewEngine returns a new Engine.
func NewEngine() *Engine {
e := Engine{render.NewConfig()}
2017-07-07 13:30:32 +02:00
filters.AddStandardFilters(&e.cfg)
2017-07-07 11:51:31 +02:00
tags.AddStandardTags(e.cfg)
return &e
}
// RegisterBlock defines a block e.g. {% tag %}…{% endtag %}.
func (e *Engine) RegisterBlock(name string, td Renderer) {
2017-07-07 11:51:31 +02:00
e.cfg.AddBlock(name).Renderer(func(w io.Writer, ctx render.Context) error {
s, err := td(ctx)
if err != nil {
return err
}
_, err = w.Write([]byte(s))
return err
})
}
// RegisterFilter defines a Liquid filter, for use as `{{ value | my_filter }}` or `{{ value | my_filter: arg }}`.
//
// A filter is a function that takes at least one input, and returns one or two outputs.
// If it returns two outputs, the second must have type error.
//
// Examples:
//
// * https://github.com/osteele/liquid/blob/master/filters/filters.go
//
// * https://github.com/osteele/gojekyll/blob/master/filters/filters.go
//
func (e *Engine) RegisterFilter(name string, fn interface{}) {
2017-07-07 11:51:31 +02:00
e.cfg.AddFilter(name, fn)
}
// RegisterTag defines a tag e.g. {% tag %}.
//
// RegisterTag defines a tag, for use as `{% tag args %}`.
//
// Examples:
//
// * https://github.com/osteele/gojekyll/blob/master/tags/tags.go
func (e *Engine) RegisterTag(name string, td Renderer) {
2017-07-01 16:36:47 +02:00
// For simplicity, don't expose the two stage parsing/rendering process to clients.
// Client tags do everything at runtime.
2017-07-07 11:51:31 +02:00
e.cfg.AddTag(name, func(_ string) (func(io.Writer, render.Context) error, error) {
return func(w io.Writer, ctx render.Context) error {
s, err := td(ctx)
if err != nil {
return err
}
_, err = w.Write([]byte(s))
return err
}, nil
2017-07-01 16:36:47 +02:00
})
2017-06-27 13:43:42 +02:00
}
// ParseTemplate creates a new Template using the engine configuration.
func (e *Engine) ParseTemplate(text []byte) (*Template, error) {
root, err := e.cfg.Compile(string(text))
if err != nil {
return nil, err
}
return &Template{root, &e.cfg}, nil
}
// ParseAndRender parses and then renders the template.
func (e *Engine) ParseAndRender(text []byte, b Bindings) ([]byte, error) {
tpl, err := e.ParseTemplate(text)
if err != nil {
return nil, err
}
return tpl.Render(b)
}
// ParseAndRenderString is a convenience wrapper for ParseAndRender, that has string input and output.
func (e *Engine) ParseAndRenderString(text string, b Bindings) (string, error) {
2017-07-03 03:17:04 +02:00
bs, err := e.ParseAndRender([]byte(text), b)
2017-06-26 16:36:53 +02:00
if err != nil {
return "", err
}
2017-07-03 03:17:04 +02:00
return string(bs), nil
}