1
0
mirror of https://github.com/danog/liquid.git synced 2024-11-27 12:35:16 +01:00
liquid/chunks/render.go

71 lines
1.8 KiB
Go
Raw Normal View History

2017-06-26 15:36:52 +02:00
package chunks
2017-06-25 17:23:20 +02:00
2017-06-25 23:00:00 +02:00
import (
"fmt"
"io"
2017-06-27 23:29:50 +02:00
"github.com/osteele/liquid/generics"
2017-06-25 23:00:00 +02:00
)
2017-06-25 17:23:20 +02:00
2017-06-26 18:41:41 +02:00
// Render evaluates an AST node and writes the result to an io.Writer.
2017-06-25 17:23:20 +02:00
func (n *ASTSeq) Render(w io.Writer, ctx Context) error {
for _, c := range n.Children {
if err := c.Render(w, ctx); err != nil {
return err
}
}
return nil
}
2017-06-26 18:41:41 +02:00
// Render evaluates an AST node and writes the result to an io.Writer.
// TODO probably safe to remove this type and method, once the test suite is larger
2017-06-25 17:23:20 +02:00
func (n *ASTChunks) Render(w io.Writer, _ Context) error {
2017-06-26 21:36:05 +02:00
fmt.Println(MustYAML(n))
return fmt.Errorf("unimplemented: ASTChunks.Render")
}
// Render evaluates an AST node and writes the result to an io.Writer.
func (n *ASTGenericTag) Render(w io.Writer, ctx Context) error {
return n.render(w, ctx)
2017-06-25 17:23:20 +02:00
}
2017-06-26 18:41:41 +02:00
// Render evaluates an AST node and writes the result to an io.Writer.
2017-06-25 17:23:20 +02:00
func (n *ASTText) Render(w io.Writer, _ Context) error {
2017-06-27 17:19:12 +02:00
_, err := w.Write([]byte(n.Source))
2017-06-25 17:23:20 +02:00
return err
}
2017-06-27 17:39:32 +02:00
// RenderASTSequence renders a sequence of nodes.
func (ctx Context) RenderASTSequence(w io.Writer, seq []ASTNode) error {
2017-06-25 23:00:00 +02:00
for _, n := range seq {
if err := n.Render(w, ctx); err != nil {
return err
}
}
return nil
2017-06-25 17:23:20 +02:00
}
2017-06-26 18:41:41 +02:00
// Render evaluates an AST node and writes the result to an io.Writer.
2017-06-25 23:00:00 +02:00
func (n *ASTControlTag) Render(w io.Writer, ctx Context) error {
cd, ok := findControlTagDefinition(n.Tag)
2017-06-27 18:06:24 +02:00
if !ok || cd.action == nil {
2017-06-27 17:19:12 +02:00
return fmt.Errorf("unimplemented tag: %s", n.Tag)
2017-06-25 23:00:00 +02:00
}
2017-06-27 17:39:32 +02:00
f := cd.action(*n)
2017-06-27 17:12:58 +02:00
return f(w, ctx)
2017-06-25 23:00:00 +02:00
}
2017-06-26 18:41:41 +02:00
// Render evaluates an AST node and writes the result to an io.Writer.
2017-06-25 23:00:00 +02:00
func (n *ASTObject) Render(w io.Writer, ctx Context) error {
// TODO separate this into parse and evaluate stages.
2017-06-27 23:29:50 +02:00
value, err := ctx.EvaluateExpr(n.Args)
2017-06-25 23:00:00 +02:00
if err != nil {
return err
}
2017-06-27 23:29:50 +02:00
if generics.IsEmpty(value) {
return nil
}
_, err = w.Write([]byte(fmt.Sprint(value)))
2017-06-25 17:23:20 +02:00
return err
}