2017-06-26 16:15:01 +02:00
|
|
|
package liquid
|
|
|
|
|
|
|
|
import (
|
2017-06-27 13:43:42 +02:00
|
|
|
"io"
|
2017-06-26 16:15:01 +02:00
|
|
|
|
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-06-26 16:15:01 +02:00
|
|
|
)
|
|
|
|
|
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.
|
2017-07-10 15:16:35 +02:00
|
|
|
type Engine struct{ cfg render.Config }
|
2017-07-07 11:51:31 +02:00
|
|
|
|
2017-07-10 15:16:35 +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)
|
2017-07-10 15:16:35 +02:00
|
|
|
return &e
|
2017-06-26 16:15:01 +02:00
|
|
|
}
|
|
|
|
|
2017-07-10 15:16:35 +02:00
|
|
|
// 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 {
|
2017-07-04 13:41:17 +02:00
|
|
|
s, err := td(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-07-16 03:13:21 +02:00
|
|
|
_, err = io.WriteString(w, s)
|
2017-07-04 13:41:17 +02:00
|
|
|
return err
|
|
|
|
})
|
2017-06-30 14:04:31 +02:00
|
|
|
}
|
|
|
|
|
2017-07-10 15:16:35 +02:00
|
|
|
// 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)
|
2017-06-27 22:02:05 +02:00
|
|
|
}
|
|
|
|
|
2017-07-10 15:16:35 +02:00
|
|
|
// RegisterTag defines a tag e.g. {% tag %}.
|
|
|
|
//
|
2017-07-14 02:25:12 +02:00
|
|
|
// Further examples are in https://github.com/osteele/gojekyll/blob/master/tags/tags.go
|
2017-07-10 15:16:35 +02:00
|
|
|
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) {
|
2017-07-04 17:41:45 +02:00
|
|
|
return func(w io.Writer, ctx render.Context) error {
|
2017-07-04 13:41:17 +02:00
|
|
|
s, err := td(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-07-16 03:13:21 +02:00
|
|
|
_, err = io.WriteString(w, s)
|
2017-07-04 13:41:17 +02:00
|
|
|
return err
|
|
|
|
}, nil
|
2017-07-01 16:36:47 +02:00
|
|
|
})
|
2017-06-27 13:43:42 +02:00
|
|
|
}
|
|
|
|
|
2017-07-10 15:16:35 +02:00
|
|
|
// ParseTemplate creates a new Template using the engine configuration.
|
2017-07-10 17:52:14 +02:00
|
|
|
func (e *Engine) ParseTemplate(source []byte) (*Template, SourceError) {
|
2017-07-14 16:17:34 +02:00
|
|
|
return newTemplate(&e.cfg, source, "", 0)
|
|
|
|
}
|
|
|
|
|
2017-07-14 16:38:30 +02:00
|
|
|
// ParseTemplateLocation is the same as ParseTemplate, except that the source location is used
|
|
|
|
// for error reporting and for the {% include %} tag.
|
|
|
|
//
|
|
|
|
// The path and line number are used for error reporting.
|
|
|
|
// The path is also the reference for relative pathnames in the {% include %} tag.
|
2017-07-14 16:17:34 +02:00
|
|
|
func (e *Engine) ParseTemplateLocation(source []byte, path string, line int) (*Template, SourceError) {
|
|
|
|
return newTemplate(&e.cfg, source, path, line)
|
2017-06-26 16:15:01 +02:00
|
|
|
}
|
|
|
|
|
2017-07-10 15:16:35 +02:00
|
|
|
// ParseAndRender parses and then renders the template.
|
2017-07-10 17:52:14 +02:00
|
|
|
func (e *Engine) ParseAndRender(source []byte, b Bindings) ([]byte, SourceError) {
|
2017-07-10 15:38:46 +02:00
|
|
|
tpl, err := e.ParseTemplate(source)
|
2017-06-26 16:15:01 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-07-10 15:16:35 +02:00
|
|
|
return tpl.Render(b)
|
2017-06-26 16:15:01 +02:00
|
|
|
}
|
|
|
|
|
2017-07-10 15:38:46 +02:00
|
|
|
// ParseAndRenderString is a convenience wrapper for ParseAndRender, that takes string input and returns a string.
|
2017-07-10 17:52:14 +02:00
|
|
|
func (e *Engine) ParseAndRenderString(source string, b Bindings) (string, SourceError) {
|
2017-07-10 15:38:46 +02:00
|
|
|
bs, err := e.ParseAndRender([]byte(source), 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
|
2017-06-26 16:15:01 +02:00
|
|
|
}
|