mirror of
https://github.com/danog/liquid.git
synced 2024-11-26 21:14:45 +01:00
simplify
This commit is contained in:
parent
903acb8d2e
commit
c599761c6a
40
engine.go
40
engine.go
@ -8,19 +8,43 @@ import (
|
||||
"github.com/osteele/liquid/tags"
|
||||
)
|
||||
|
||||
type engine struct{ settings render.Config }
|
||||
// An Engine parses template source into renderable text.
|
||||
//
|
||||
// An engine can be configured with additional filters and tags.
|
||||
//
|
||||
// Filters
|
||||
//
|
||||
// RegisterFilter defines a Liquid filter.
|
||||
//
|
||||
// A filter is any function that takes at least one input, and returns one or two outputs.
|
||||
// If it returns two outputs, the second must be an error.
|
||||
type Engine interface {
|
||||
// RegisterFilter defines a filter function e.g. {{ value | filter: arg }}.
|
||||
RegisterFilter(name string, fn interface{})
|
||||
// RegisterTag defines a tag function e.g. {% tag %}.
|
||||
RegisterTag(string, Renderer)
|
||||
RegisterBlock(string, Renderer)
|
||||
|
||||
ParseTemplate([]byte) (Template, error)
|
||||
// ParseAndRender parses and then renders the template.
|
||||
ParseAndRender([]byte, Bindings) ([]byte, error)
|
||||
// ParseAndRenderString is a convenience wrapper for ParseAndRender, that has string input and output.
|
||||
ParseAndRenderString(string, Bindings) (string, error)
|
||||
}
|
||||
|
||||
type engine struct{ cfg render.Config }
|
||||
|
||||
// NewEngine returns a new template engine.
|
||||
func NewEngine() Engine {
|
||||
e := engine{render.NewConfig()}
|
||||
filters.AddStandardFilters(&e.settings.Config.Config)
|
||||
tags.AddStandardTags(e.settings)
|
||||
filters.AddStandardFilters(&e.cfg.Config.Config)
|
||||
tags.AddStandardTags(e.cfg)
|
||||
return e
|
||||
}
|
||||
|
||||
// RegisterBlock is in the Engine interface.
|
||||
func (e engine) RegisterBlock(name string, td Renderer) {
|
||||
e.settings.AddBlock(name).Renderer(func(w io.Writer, ctx render.Context) error {
|
||||
e.cfg.AddBlock(name).Renderer(func(w io.Writer, ctx render.Context) error {
|
||||
s, err := td(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
@ -32,14 +56,14 @@ func (e engine) RegisterBlock(name string, td Renderer) {
|
||||
|
||||
// RegisterFilter is in the Engine interface.
|
||||
func (e engine) RegisterFilter(name string, fn interface{}) {
|
||||
e.settings.AddFilter(name, fn)
|
||||
e.cfg.AddFilter(name, fn)
|
||||
}
|
||||
|
||||
// RegisterTag is in the Engine interface.
|
||||
func (e engine) RegisterTag(name string, td Renderer) {
|
||||
// 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, render.Context) error, error) {
|
||||
e.cfg.AddTag(name, func(_ string) (func(io.Writer, render.Context) error, error) {
|
||||
return func(w io.Writer, ctx render.Context) error {
|
||||
s, err := td(ctx)
|
||||
if err != nil {
|
||||
@ -53,11 +77,11 @@ func (e engine) RegisterTag(name string, td Renderer) {
|
||||
|
||||
// ParseTemplate is in the Engine interface.
|
||||
func (e engine) ParseTemplate(text []byte) (Template, error) {
|
||||
ast, err := e.settings.Compile(string(text))
|
||||
ast, err := e.cfg.Compile(string(text))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &template{ast, &e.settings}, nil
|
||||
return &template{ast, &e.cfg}, nil
|
||||
}
|
||||
|
||||
// ParseAndRender is in the Engine interface.
|
||||
|
@ -2,10 +2,10 @@ package expression
|
||||
|
||||
// Config holds configuration information for expression interpretation.
|
||||
type Config struct {
|
||||
filterDictionary
|
||||
filters map[string]interface{}
|
||||
}
|
||||
|
||||
// NewConfig creates a new Settings.
|
||||
func NewConfig() Config {
|
||||
return Config{newFilterDictionary()}
|
||||
return Config{}
|
||||
}
|
||||
|
@ -23,16 +23,8 @@ func (e UndefinedFilter) Error() string {
|
||||
|
||||
type valueFn func(Context) interface{}
|
||||
|
||||
type filterDictionary struct {
|
||||
filters map[string]interface{}
|
||||
}
|
||||
|
||||
func newFilterDictionary() filterDictionary {
|
||||
return filterDictionary{map[string]interface{}{}}
|
||||
}
|
||||
|
||||
// AddFilter adds a filter to the filter dictionary.
|
||||
func (d *filterDictionary) AddFilter(name string, fn interface{}) {
|
||||
func (c *Config) AddFilter(name string, fn interface{}) {
|
||||
rf := reflect.ValueOf(fn)
|
||||
switch {
|
||||
case rf.Kind() != reflect.Func:
|
||||
@ -44,10 +36,10 @@ func (d *filterDictionary) AddFilter(name string, fn interface{}) {
|
||||
// case rf.Type().Out(1).Implements(…):
|
||||
// panic(typeError("a filter's second output must be type error"))
|
||||
}
|
||||
if len(d.filters) == 0 {
|
||||
d.filters = make(map[string]interface{})
|
||||
if len(c.filters) == 0 {
|
||||
c.filters = make(map[string]interface{})
|
||||
}
|
||||
d.filters[name] = fn
|
||||
c.filters[name] = fn
|
||||
}
|
||||
|
||||
var closureType = reflect.TypeOf(closure{})
|
||||
|
33
liquid.go
33
liquid.go
@ -15,39 +15,6 @@ import (
|
||||
"github.com/osteele/liquid/render"
|
||||
)
|
||||
|
||||
// An Engine parses template source into renderable text.
|
||||
//
|
||||
// An engine can be configured with additional filters and tags.
|
||||
//
|
||||
// Filters
|
||||
//
|
||||
// RegisterFilter defines a Liquid filter.
|
||||
//
|
||||
// A filter is any function that takes at least one input, and returns one or two outputs.
|
||||
// If it returns two outputs, the second must be an error.
|
||||
type Engine interface {
|
||||
// RegisterFilter defines a filter function e.g. {{ value | filter: arg }}.
|
||||
RegisterFilter(name string, fn interface{})
|
||||
// RegisterTag defines a tag function e.g. {% tag %}.
|
||||
RegisterTag(string, Renderer)
|
||||
RegisterBlock(string, Renderer)
|
||||
|
||||
ParseTemplate([]byte) (Template, error)
|
||||
// ParseAndRender parses and then renders the template.
|
||||
ParseAndRender([]byte, Bindings) ([]byte, error)
|
||||
// ParseAndRenderString is a convenience wrapper for ParseAndRender, that has string input and output.
|
||||
ParseAndRenderString(string, Bindings) (string, error)
|
||||
}
|
||||
|
||||
// A Template renders a template according to scope.
|
||||
type Template interface {
|
||||
// Render executes the template with the specified bindings.
|
||||
Render(Bindings) ([]byte, error)
|
||||
// RenderString is a convenience wrapper for Render, that has string input and output.
|
||||
RenderString(Bindings) (string, error)
|
||||
SetSourcePath(string)
|
||||
}
|
||||
|
||||
// Bindings is a map of variable names to values.
|
||||
type Bindings map[string]interface{}
|
||||
|
||||
|
@ -5,11 +5,10 @@ import "github.com/osteele/liquid/expression"
|
||||
// // Config holds configuration information for parsing and rendering.
|
||||
type Config struct {
|
||||
expression.Config
|
||||
// Filename string
|
||||
Grammar Grammar
|
||||
}
|
||||
|
||||
// NewConfig creates a new Settings.
|
||||
func NewConfig() Config {
|
||||
return Config{Config: expression.NewConfig()}
|
||||
return Config{}
|
||||
}
|
||||
|
@ -15,15 +15,9 @@ type Config struct {
|
||||
// NewConfig creates a new Settings.
|
||||
func NewConfig() Config {
|
||||
c := Config{
|
||||
// Config: parser.NewConfig(),
|
||||
tags: map[string]TagCompiler{},
|
||||
blockDefs: map[string]*blockSyntax{},
|
||||
}
|
||||
c.Grammar = c
|
||||
return c
|
||||
}
|
||||
|
||||
// AddFilter adds a filter to settings.
|
||||
// func (s Config) AddFilter(name string, fn interface{}) {
|
||||
// s.Config.AddFilter(name, fn)
|
||||
// }
|
||||
|
@ -6,6 +6,15 @@ import (
|
||||
"github.com/osteele/liquid/render"
|
||||
)
|
||||
|
||||
// A Template renders a template according to scope.
|
||||
type Template interface {
|
||||
// Render executes the template with the specified bindings.
|
||||
Render(Bindings) ([]byte, error)
|
||||
// RenderString is a convenience wrapper for Render, that has string input and output.
|
||||
RenderString(Bindings) (string, error)
|
||||
SetSourcePath(string)
|
||||
}
|
||||
|
||||
type template struct {
|
||||
root render.Node
|
||||
config *render.Config
|
||||
|
Loading…
Reference in New Issue
Block a user