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

42 lines
992 B
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:
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))
}