2017-07-14 02:25:12 +02:00
|
|
|
// Package render renders a compiled template parse tree.
|
2017-07-04 17:03:18 +02:00
|
|
|
package render
|
2017-06-25 17:23:20 +02:00
|
|
|
|
2017-06-25 23:00:00 +02:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2017-06-29 19:41:26 +02:00
|
|
|
"reflect"
|
2017-07-03 18:00:43 +02:00
|
|
|
|
2017-07-05 17:35:07 +02:00
|
|
|
"github.com/osteele/liquid/evaluator"
|
2017-06-25 23:00:00 +02:00
|
|
|
)
|
2017-06-25 17:23:20 +02:00
|
|
|
|
2017-07-06 15:21:26 +02:00
|
|
|
// Render renders the render tree.
|
2017-07-10 17:49:14 +02:00
|
|
|
func Render(node Node, w io.Writer, vars map[string]interface{}, c Config) Error {
|
2017-07-14 19:01:04 +02:00
|
|
|
return node.render(w, newNodeContext(vars, c))
|
2017-06-25 17:23:20 +02:00
|
|
|
}
|
|
|
|
|
2017-07-14 19:01:04 +02:00
|
|
|
func (n *BlockNode) render(w io.Writer, ctx nodeContext) Error {
|
|
|
|
cd, ok := ctx.config.findBlockDef(n.Name)
|
|
|
|
if !ok || cd.parser == nil {
|
|
|
|
// this should have been detected during compilation; it's an implementation error if it happens here
|
|
|
|
panic(fmt.Errorf("unknown tag: %s", n.Name))
|
|
|
|
}
|
|
|
|
renderer := n.renderer
|
|
|
|
if renderer == nil {
|
|
|
|
panic(fmt.Errorf("unset renderer for %v", n))
|
|
|
|
}
|
|
|
|
err := renderer(w, rendererContext{ctx, nil, n})
|
|
|
|
return wrapRenderError(err, n)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *RawNode) render(w io.Writer, ctx nodeContext) Error {
|
|
|
|
for _, s := range n.slices {
|
2017-07-16 03:13:21 +02:00
|
|
|
_, err := io.WriteString(w, s)
|
2017-06-27 23:40:15 +02:00
|
|
|
if err != nil {
|
2017-07-10 17:49:14 +02:00
|
|
|
return wrapRenderError(err, n)
|
2017-06-27 23:40:15 +02:00
|
|
|
}
|
2017-07-14 19:01:04 +02:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *ObjectNode) render(w io.Writer, ctx nodeContext) Error {
|
|
|
|
value, err := ctx.Evaluate(n.expr)
|
|
|
|
if err != nil {
|
2017-07-10 17:49:14 +02:00
|
|
|
return wrapRenderError(err, n)
|
2017-07-14 19:01:04 +02:00
|
|
|
}
|
|
|
|
return wrapRenderError(writeObject(value, w), n)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *SeqNode) render(w io.Writer, ctx nodeContext) Error {
|
|
|
|
for _, c := range n.Children {
|
|
|
|
if err := c.render(w, ctx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-06-27 23:40:15 +02:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-07-14 19:01:04 +02:00
|
|
|
func (n *TagNode) render(w io.Writer, ctx nodeContext) Error {
|
|
|
|
return wrapRenderError(n.renderer(w, rendererContext{ctx, n, nil}), n)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *TextNode) render(w io.Writer, ctx nodeContext) Error {
|
2017-07-16 03:13:21 +02:00
|
|
|
_, err := io.WriteString(w, n.Source)
|
2017-07-14 19:01:04 +02:00
|
|
|
return wrapRenderError(err, n)
|
|
|
|
}
|
|
|
|
|
2017-06-30 20:51:21 +02:00
|
|
|
// writeObject writes a value used in an object node
|
|
|
|
func writeObject(value interface{}, w io.Writer) error {
|
2017-07-05 17:35:07 +02:00
|
|
|
value = evaluator.ToLiquid(value)
|
2017-06-29 19:41:26 +02:00
|
|
|
if value == nil {
|
2017-06-27 23:29:50 +02:00
|
|
|
return nil
|
|
|
|
}
|
2017-06-29 19:41:26 +02:00
|
|
|
rt := reflect.ValueOf(value)
|
|
|
|
switch rt.Kind() {
|
|
|
|
case reflect.Array, reflect.Slice:
|
|
|
|
for i := 0; i < rt.Len(); i++ {
|
|
|
|
item := rt.Index(i)
|
|
|
|
if item.IsValid() {
|
2017-06-30 20:51:21 +02:00
|
|
|
if err := writeObject(item.Interface(), w); err != nil {
|
2017-06-29 19:41:26 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
default:
|
2017-07-16 03:13:21 +02:00
|
|
|
_, err := io.WriteString(w, fmt.Sprint(value))
|
2017-06-29 19:41:26 +02:00
|
|
|
return err
|
|
|
|
}
|
2017-06-25 17:23:20 +02:00
|
|
|
}
|
2017-06-28 02:23:09 +02:00
|
|
|
|
|
|
|
// RenderASTSequence renders a sequence of nodes.
|
2017-07-10 17:49:14 +02:00
|
|
|
func (c nodeContext) RenderSequence(w io.Writer, seq []Node) Error {
|
2017-06-28 02:23:09 +02:00
|
|
|
for _, n := range seq {
|
2017-07-14 19:01:04 +02:00
|
|
|
if err := n.render(w, c); err != nil {
|
2017-06-28 02:23:09 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|