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

66 lines
1.7 KiB
Go
Raw Normal View History

package liquid
import (
2017-06-27 13:43:42 +02:00
"io"
"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-30 22:13:18 +02:00
type engine struct{ settings chunks.Settings }
2017-06-29 19:08:25 +02:00
// NewEngine returns a new template engine.
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
}
// 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)
}
// 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)
}
// 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))
if err != nil {
return nil, err
}
2017-06-30 22:13:18 +02:00
return &template{ast, e.settings}, nil
}
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)
if err != nil {
return nil, err
}
2017-07-02 05:52:38 +02:00
return t.Render(c)
}
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
}