1
0
mirror of https://github.com/danog/liquid.git synced 2025-01-23 09:21:11 +01:00
liquid/chunks/render.go

90 lines
2.1 KiB
Go
Raw Normal View History

2017-06-26 09:36:52 -04:00
package chunks
2017-06-25 11:23:20 -04:00
2017-06-25 17:00:00 -04:00
import (
"fmt"
"io"
)
2017-06-25 11:23:20 -04:00
2017-06-26 12:41:41 -04:00
// Render evaluates an AST node and writes the result to an io.Writer.
2017-06-25 11:23:20 -04: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 12:41:41 -04:00
// Render evaluates an AST node and writes the result to an io.Writer.
2017-06-25 11:23:20 -04:00
func (n *ASTChunks) Render(w io.Writer, _ Context) error {
2017-06-26 15:36:05 -04: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 11:23:20 -04:00
}
2017-06-26 12:41:41 -04:00
// Render evaluates an AST node and writes the result to an io.Writer.
2017-06-25 11:23:20 -04:00
func (n *ASTText) Render(w io.Writer, _ Context) error {
_, err := w.Write([]byte(n.chunk.Source))
return err
}
2017-06-26 12:41:41 -04:00
// Render evaluates an AST node and writes the result to an io.Writer.
func renderASTSequence(w io.Writer, seq []ASTNode, ctx Context) error {
2017-06-25 17:00:00 -04:00
for _, n := range seq {
if err := n.Render(w, ctx); err != nil {
return err
}
}
return nil
2017-06-25 11:23:20 -04:00
}
2017-06-26 12:41:41 -04:00
// Render evaluates an AST node and writes the result to an io.Writer.
2017-06-25 17:00:00 -04:00
func (n *ASTControlTag) Render(w io.Writer, ctx Context) error {
switch n.chunk.Tag {
2017-06-25 20:11:29 -04:00
case "if", "unless":
2017-06-26 09:33:07 -04:00
val, err := ctx.EvaluateExpr(n.chunk.Args)
2017-06-25 17:00:00 -04:00
if err != nil {
return err
}
2017-06-25 20:11:29 -04:00
if n.chunk.Tag == "unless" {
val = (val == nil || val == false)
}
2017-06-25 17:26:14 -04:00
switch val {
default:
2017-06-26 12:41:41 -04:00
return renderASTSequence(w, n.body, ctx)
2017-06-25 17:26:14 -04:00
case nil, false:
for _, c := range n.branches {
switch c.chunk.Tag {
case "else":
val = true
case "elsif":
2017-06-26 09:33:07 -04:00
val, err = ctx.EvaluateExpr(c.chunk.Args)
2017-06-25 17:26:14 -04:00
if err != nil {
return err
}
}
if val != nil && val != false {
2017-06-26 12:41:41 -04:00
return renderASTSequence(w, c.body, ctx)
2017-06-25 17:26:14 -04:00
}
}
2017-06-25 17:00:00 -04:00
}
return nil
default:
2017-06-26 15:36:05 -04:00
return fmt.Errorf("unimplemented tag: %s", n.chunk.Tag)
2017-06-25 17:00:00 -04:00
}
}
2017-06-26 12:41:41 -04:00
// Render evaluates an AST node and writes the result to an io.Writer.
2017-06-25 17:00:00 -04:00
func (n *ASTObject) Render(w io.Writer, ctx Context) error {
2017-06-26 12:41:41 -04:00
val, err := ctx.EvaluateExpr(n.chunk.Args)
2017-06-25 17:00:00 -04:00
if err != nil {
return err
}
_, err = w.Write([]byte(fmt.Sprint(val)))
2017-06-25 11:23:20 -04:00
return err
}