1
0
mirror of https://github.com/danog/liquid.git synced 2025-01-06 01:38:27 +01:00
liquid/chunks/context.go

32 lines
863 B
Go
Raw Normal View History

2017-06-26 21:36:05 +02:00
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) (interface{}, error) {
return expressions.EvaluateExpr(source, expressions.NewContext(c.vars))
}
func (c *Context) evaluateStatement(tag, source string) (interface{}, error) {
return expressions.EvaluateExpr(fmt.Sprintf("%%%s %s", tag, source), expressions.NewContext(c.vars))
}