1
0
mirror of https://github.com/danog/liquid.git synced 2024-11-27 07:24:38 +01:00
liquid/chunks/context.go
2017-06-29 07:54:31 -04:00

87 lines
2.3 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{}
}
// NewContext creates a new evaluation context.
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}
}
// GetVariableMap returns the variable map. This is required by some tangled code
// in Jekyll includes, that should hopefully get better.
func (c Context) GetVariableMap() map[string]interface{} {
return c.vars
}
// Get gets a variable value within an evaluation context.
func (c Context) Get(name string) interface{} {
return c.vars[name]
}
// Set sets a variable value from an evaluation context.
func (c Context) Set(name string, value interface{}) {
c.vars[name] = value
}
// 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))
}
// EvaluateString evaluates an expression within the template context.
func (c Context) EvaluateString(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(fmt.Errorf("%s during evaluation of %s", e, source))
}
}
}()
return expressions.EvaluateString(source, expressions.NewContext(c.vars))
}
func (c Context) evaluateStatement(tag, source string) (interface{}, error) {
return c.EvaluateString(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
}