1
0
mirror of https://github.com/danog/liquid.git synced 2024-11-27 05:34:46 +01:00
liquid/template.go
2017-06-28 20:49:38 -04:00

37 lines
1.0 KiB
Go

package liquid
import (
"bytes"
"github.com/osteele/liquid/chunks"
)
// Template renders a template according to scope.
//
// Bindings is a map of liquid variable names to objects.
type Template interface {
// Render executes the template with the specified bindings.
Render(bindings map[string]interface{}) ([]byte, error)
// RenderString is a convenience wrapper for Render, that has string input and output.
RenderString(bindings map[string]interface{}) (string, error)
}
// Render executes the template within the bindings environment.
func (t *template) Render(bindings map[string]interface{}) ([]byte, error) {
buf := new(bytes.Buffer)
err := t.ast.Render(buf, chunks.NewContext(bindings))
if err != nil {
return nil, err
}
return buf.Bytes(), nil
}
// RenderString is a convenience wrapper for Render, that has string input and output.
func (t *template) RenderString(bindings map[string]interface{}) (string, error) {
b, err := t.Render(bindings)
if err != nil {
return "", err
}
return string(b), nil
}