1
0
mirror of https://github.com/danog/liquid.git synced 2024-11-26 21:24:40 +01:00
liquid/template.go

51 lines
1.2 KiB
Go
Raw Normal View History

2017-06-27 13:43:42 +02:00
package liquid
import (
"bytes"
2017-07-04 17:03:18 +02:00
"github.com/osteele/liquid/render"
2017-06-27 13:43:42 +02:00
)
2017-07-07 11:51:31 +02:00
// A Template renders a template according to scope.
type Template interface {
// Render executes the template with the specified bindings.
Render(Bindings) ([]byte, error)
// RenderString is a convenience wrapper for Render, that has string input and output.
RenderString(Bindings) (string, error)
SetSourcePath(string)
2017-07-09 17:49:24 +02:00
SetSourceLocation(string, int)
2017-07-07 11:51:31 +02:00
}
2017-06-30 22:13:18 +02:00
type template struct {
2017-07-07 11:41:37 +02:00
root render.Node
config *render.Config
2017-06-30 22:13:18 +02:00
}
2017-06-29 02:49:38 +02:00
// Render executes the template within the bindings environment.
2017-07-07 11:41:37 +02:00
func (t *template) Render(vars Bindings) ([]byte, error) {
2017-06-27 13:43:42 +02:00
buf := new(bytes.Buffer)
2017-07-07 11:41:37 +02:00
err := render.Render(t.root, buf, vars, *t.config)
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
}
2017-07-04 22:48:38 +02:00
func (t *template) SetSourcePath(filename string) {
t.config.Filename = filename
}
2017-07-09 17:49:24 +02:00
func (t *template) SetSourceLocation(filename string, lineNo int) {
t.config.Filename = filename
t.config.LineNo = lineNo
}