mirror of
https://github.com/danog/gojekyll.git
synced 2024-11-27 11:34:46 +01:00
41 lines
1007 B
Go
41 lines
1007 B
Go
package liquid
|
|
|
|
import (
|
|
"bytes"
|
|
|
|
"github.com/acstech/liquid"
|
|
"github.com/acstech/liquid/core"
|
|
)
|
|
|
|
type Template liquid.Template
|
|
|
|
func Parse(data []byte, config *core.Configuration) (*Template, error) {
|
|
template, err := liquid.Parse(data, config)
|
|
return (*Template)(template), err
|
|
}
|
|
|
|
// Render is a wrapper around liquid's template.Render that turns panics into errors
|
|
func Render(template *Template, variables map[string]interface{}) (bs []byte, err error) {
|
|
defer func() {
|
|
if r := recover(); r != nil {
|
|
if e, ok := r.(error); ok {
|
|
err = e
|
|
} else {
|
|
panic(r)
|
|
}
|
|
}
|
|
}()
|
|
writer := new(bytes.Buffer)
|
|
(*liquid.Template)(template).Render(writer, variables)
|
|
return writer.Bytes(), nil
|
|
}
|
|
|
|
// ParseAndApplyTemplate parses and then renders the template.
|
|
func ParseAndApplyTemplate(data []byte, variables map[string]interface{}, config *core.Configuration) ([]byte, error) {
|
|
template, err := Parse(data, config)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return Render(template, variables)
|
|
}
|