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
|
|
|
|
|
|
|
"github.com/osteele/liquid/chunks"
|
2017-06-27 18:06:24 +02:00
|
|
|
"github.com/osteele/liquid/filters"
|
2017-06-30 22:46:17 +02:00
|
|
|
"github.com/osteele/liquid/tags"
|
2017-06-26 16:15:01 +02:00
|
|
|
)
|
|
|
|
|
2017-06-30 22:13:18 +02:00
|
|
|
type engine struct{ settings chunks.Settings }
|
2017-06-26 16:15:01 +02:00
|
|
|
|
2017-06-29 19:08:25 +02:00
|
|
|
// NewEngine returns a new template engine.
|
2017-06-26 16:15:01 +02:00
|
|
|
func NewEngine() Engine {
|
2017-06-30 22:13:18 +02:00
|
|
|
e := engine{chunks.NewSettings()}
|
|
|
|
filters.AddStandardFilters(e.settings.ExpressionSettings)
|
2017-06-30 22:46:17 +02:00
|
|
|
tags.AddStandardTags(e.settings)
|
2017-06-30 22:13:18 +02:00
|
|
|
return e
|
2017-06-26 16:15:01 +02:00
|
|
|
}
|
|
|
|
|
2017-07-02 19:31:34 +02:00
|
|
|
// RegisterBlock is in the Engine interface.
|
|
|
|
func (e engine) RegisterBlock(name string, td func(io.Writer, chunks.RenderContext) error) {
|
|
|
|
e.settings.AddBlock(name).Renderer(td)
|
2017-06-30 14:04:31 +02:00
|
|
|
}
|
|
|
|
|
2017-07-02 19:31:34 +02:00
|
|
|
// RegisterFilter is in the Engine interface.
|
|
|
|
func (e engine) RegisterFilter(name string, fn interface{}) {
|
2017-06-30 22:13:18 +02:00
|
|
|
e.settings.AddFilter(name, fn)
|
2017-06-27 22:02:05 +02:00
|
|
|
}
|
|
|
|
|
2017-07-02 19:31:34 +02:00
|
|
|
// RegisterTag is in the Engine interface.
|
|
|
|
func (e engine) RegisterTag(name string, td TagDefinition) {
|
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.
|
|
|
|
e.settings.AddTag(name, func(_ string) (func(io.Writer, chunks.RenderContext) error, error) {
|
|
|
|
return td, nil
|
|
|
|
})
|
2017-06-27 13:43:42 +02:00
|
|
|
}
|
|
|
|
|
2017-06-29 19:08:25 +02:00
|
|
|
// ParseTemplate is in the Engine interface.
|
2017-06-26 21:36:05 +02:00
|
|
|
func (e engine) ParseTemplate(text []byte) (Template, error) {
|
2017-06-30 22:51:39 +02:00
|
|
|
ast, err := e.settings.Parse(string(text))
|
2017-06-26 16:15:01 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-06-30 22:13:18 +02:00
|
|
|
return &template{ast, e.settings}, nil
|
2017-06-26 16:15:01 +02:00
|
|
|
}
|
|
|
|
|
2017-06-29 19:08:25 +02:00
|
|
|
// ParseAndRender is in the Engine interface.
|
2017-07-02 05:52:38 +02:00
|
|
|
func (e engine) ParseAndRender(text []byte, c Context) ([]byte, error) {
|
2017-06-26 21:36:05 +02:00
|
|
|
t, err := e.ParseTemplate(text)
|
2017-06-26 16:15:01 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-07-02 05:52:38 +02:00
|
|
|
return t.Render(c)
|
2017-06-26 16:15:01 +02:00
|
|
|
}
|
|
|
|
|
2017-06-29 19:08:25 +02:00
|
|
|
// ParseAndRenderString is in the Engine interface.
|
2017-07-02 05:52:38 +02:00
|
|
|
func (e engine) ParseAndRenderString(text string, c Context) (string, error) {
|
|
|
|
b, err := e.ParseAndRender([]byte(text), c)
|
2017-06-26 16:36:53 +02:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return string(b), nil
|
2017-06-26 16:15:01 +02:00
|
|
|
}
|