2017-06-26 16:15:01 +02:00
|
|
|
package liquid
|
|
|
|
|
|
|
|
import (
|
2017-06-30 14:04:31 +02:00
|
|
|
"fmt"
|
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-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)
|
|
|
|
return e
|
2017-06-26 16:15:01 +02:00
|
|
|
}
|
|
|
|
|
2017-06-30 14:04:31 +02:00
|
|
|
// DefineStartTag is in the Engine interface.
|
|
|
|
func (e engine) DefineStartTag(name string, td TagDefinition) {
|
2017-06-30 20:51:21 +02:00
|
|
|
chunks.DefineStartTag(name).Parser(func(c chunks.ASTControlTag) (func(io.Writer, chunks.RenderContext) error, error) {
|
|
|
|
return func(io.Writer, chunks.RenderContext) error {
|
2017-06-30 14:04:31 +02:00
|
|
|
fmt.Println("unimplemented tag:", name)
|
|
|
|
return nil
|
|
|
|
}, nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-06-29 19:08:25 +02:00
|
|
|
// DefineFilter is in the Engine interface.
|
2017-06-27 22:02:05 +02:00
|
|
|
func (e engine) DefineFilter(name string, fn interface{}) {
|
|
|
|
// TODO define this on the engine, not globally
|
2017-06-30 22:13:18 +02:00
|
|
|
e.settings.AddFilter(name, fn)
|
2017-06-27 22:02:05 +02:00
|
|
|
}
|
|
|
|
|
2017-06-29 19:08:25 +02:00
|
|
|
// ParseAndRenderString is in the Engine interface.
|
|
|
|
func (e engine) DefineTag(name string, td TagDefinition) {
|
2017-06-27 22:02:05 +02:00
|
|
|
// TODO define this on the engine, not globally
|
2017-06-27 13:43:42 +02:00
|
|
|
chunks.DefineTag(name, chunks.TagDefinition(td))
|
|
|
|
}
|
|
|
|
|
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-26 16:15:01 +02:00
|
|
|
tokens := chunks.Scan(string(text), "")
|
|
|
|
ast, err := chunks.Parse(tokens)
|
|
|
|
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-06-29 02:49:38 +02:00
|
|
|
func (e engine) ParseAndRender(text []byte, bindings map[string]interface{}) ([]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-06-29 02:49:38 +02:00
|
|
|
return t.Render(bindings)
|
2017-06-26 16:15:01 +02:00
|
|
|
}
|
|
|
|
|
2017-06-29 19:08:25 +02:00
|
|
|
// ParseAndRenderString is in the Engine interface.
|
2017-06-29 02:49:38 +02:00
|
|
|
func (e engine) ParseAndRenderString(text string, bindings map[string]interface{}) (string, error) {
|
|
|
|
b, err := e.ParseAndRender([]byte(text), bindings)
|
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
|
|
|
}
|