1
0
mirror of https://github.com/danog/liquid.git synced 2024-12-12 17:18:13 +01:00
liquid/chunks/context.go
2017-06-27 11:39:32 -04:00

54 lines
1.4 KiB
Go

package chunks
import (
"fmt"
"github.com/osteele/liquid/expressions"
)
// Context is the evaluation context for chunk AST rendering.
type Context struct {
vars map[string]interface{}
}
func NewContext(scope map[string]interface{}) 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}
}
// EvaluateExpr evaluates an expression within the template context.
func (c *Context) EvaluateExpr(source string) (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 expressions.EvaluateExpr(source, expressions.NewContext(c.vars))
}
func (c *Context) evaluateStatement(tag, source string) (interface{}, error) {
return c.EvaluateExpr(fmt.Sprintf("%%%s %s", tag, source))
}
// MakeExpressionValueFn parses source into an evaluation function
func MakeExpressionValueFn(source string) (func(Context) (interface{}, error), error) {
expr, err := expressions.Parse(source)
if err != nil {
return nil, err
}
return func(ctx Context) (interface{}, error) {
return expr.Evaluate(expressions.NewContext(ctx.vars))
}, nil
}