2017-06-27 13:43:42 +02:00
|
|
|
package liquid
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
|
|
|
|
"github.com/osteele/liquid/chunks"
|
|
|
|
)
|
|
|
|
|
2017-06-30 22:13:18 +02:00
|
|
|
type template struct {
|
|
|
|
ast chunks.ASTNode
|
|
|
|
settings chunks.Settings
|
|
|
|
}
|
|
|
|
|
2017-06-29 02:49:38 +02:00
|
|
|
// Render executes the template within the bindings environment.
|
2017-07-03 03:17:04 +02:00
|
|
|
func (t *template) Render(b Bindings) ([]byte, error) {
|
2017-06-27 13:43:42 +02:00
|
|
|
buf := new(bytes.Buffer)
|
2017-07-04 16:25:29 +02:00
|
|
|
err := chunks.Render(t.ast, buf, chunks.NewContext(b, t.settings))
|
2017-06-27 13:43:42 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return buf.Bytes(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// RenderString is a convenience wrapper for Render, that has string input and output.
|
2017-07-03 03:17:04 +02:00
|
|
|
func (t *template) RenderString(b Bindings) (string, error) {
|
|
|
|
bs, err := t.Render(b)
|
2017-06-27 13:43:42 +02:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2017-07-03 03:17:04 +02:00
|
|
|
return string(bs), nil
|
2017-06-27 13:43:42 +02:00
|
|
|
}
|