1
0
mirror of https://github.com/danog/liquid.git synced 2024-11-27 15:24:40 +01:00
liquid/chunks/context.go
2017-06-30 16:13:18 -04:00

51 lines
1.2 KiB
Go

package chunks
import (
"github.com/osteele/liquid/expressions"
)
// Context is the evaluation context for chunk AST rendering.
type Context struct {
vars map[string]interface{}
settings Settings
}
type Settings struct {
ExpressionSettings expressions.Settings
}
func (s Settings) AddFilter(name string, fn interface{}) {
s.ExpressionSettings.AddFilter(name, fn)
}
func NewSettings() Settings {
return Settings{expressions.NewSettings()}
}
// NewContext creates a new evaluation context.
func NewContext(scope map[string]interface{}, s Settings) Context {
// The assign tag modifies the scope, so make a copy first.
// TODO this isn't really the right place for this.
vars := map[string]interface{}{}
for k, v := range scope {
vars[k] = v
}
return Context{vars, s}
}
// Evaluate evaluates an expression within the template context.
func (c Context) Evaluate(expr expressions.Expression) (out interface{}, err error) {
defer func() {
if r := recover(); r != nil {
switch e := r.(type) {
case expressions.InterpreterError:
err = e
default:
// fmt.Println(string(debug.Stack()))
panic(e)
}
}
}()
return expr.Evaluate(expressions.NewContext(c.vars, c.settings.ExpressionSettings))
}