1
0
mirror of https://github.com/danog/liquid.git synced 2024-11-30 05:58:59 +01:00
liquid/engine.go

77 lines
2.1 KiB
Go
Raw Normal View History

2017-06-26 16:36:53 +02:00
/*
Package liquid is a very early-stage pure Go library that implements Shopify Liquid <https://shopify.github.io/liquid> templates.
It's intended for use in for use in https://github.com/osteele/gojekyll.
*/
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"
"github.com/osteele/liquid/tags"
)
2017-06-27 18:06:24 +02:00
// TODO move the filters and tags from globals to the engine
func init() {
tags.DefineStandardTags()
filters.DefineStandardFilters()
}
2017-06-26 16:36:53 +02:00
// Engine parses template source into renderable text.
//
// In the future, it will be configured with additional tags, filters, and the {%include%} search path.
type Engine interface {
2017-06-27 13:43:42 +02:00
DefineTag(string, func(form string) (func(io.Writer, chunks.Context) error, error))
2017-06-26 21:36:05 +02:00
ParseTemplate(text []byte) (Template, error)
ParseAndRender(text []byte, scope map[string]interface{}) ([]byte, error)
2017-06-26 16:36:53 +02:00
ParseAndRenderString(text string, scope map[string]interface{}) (string, error)
}
2017-06-27 13:43:42 +02:00
type TagDefinition func(expr string) (func(io.Writer, chunks.Context) error, error)
type engine struct{}
type template struct {
2017-06-26 18:41:41 +02:00
ast chunks.ASTNode
}
2017-06-26 16:36:53 +02:00
// NewEngine makes a new engine.
func NewEngine() Engine {
return engine{}
}
2017-06-27 13:43:42 +02:00
func (e engine) DefineTag(name string, td func(form string) (func(io.Writer, chunks.Context) error, error)) {
chunks.DefineTag(name, chunks.TagDefinition(td))
}
2017-06-26 21:36:05 +02:00
func (e engine) ParseTemplate(text []byte) (Template, error) {
tokens := chunks.Scan(string(text), "")
ast, err := chunks.Parse(tokens)
2017-06-26 21:36:05 +02:00
// fmt.Println(chunks.MustYAML(ast))
if err != nil {
return nil, err
}
return &template{ast}, nil
}
// ParseAndRender parses and then renders the template.
func (e engine) ParseAndRender(text []byte, scope map[string]interface{}) ([]byte, error) {
2017-06-26 21:36:05 +02:00
t, err := e.ParseTemplate(text)
if err != nil {
return nil, err
}
return t.Render(scope)
}
2017-06-26 16:36:53 +02:00
// ParseAndRenderString is a convenience wrapper for ParseAndRender, that has string input and output.
func (e engine) ParseAndRenderString(text string, scope map[string]interface{}) (string, error) {
b, err := e.ParseAndRender([]byte(text), scope)
if err != nil {
return "", err
}
return string(b), nil
}