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

Remove gratuitous Context wrapper

This commit is contained in:
Oliver Steele 2017-07-02 21:17:04 -04:00
parent 2e8f51ad06
commit cb8911a22c
5 changed files with 34 additions and 42 deletions

View File

@ -1,15 +1,15 @@
package liquid
type context struct {
bindings map[string]interface{}
}
// type context struct {
// bindings map[string]interface{}
// }
// NewContext creates a new context from a variable binding map.
func NewContext(bindings map[string]interface{}) Context {
return &context{bindings}
}
// // NewContext creates a new context from a variable binding map.
// func NewContext(bindings map[string]interface{}) Context {
// return &context{bindings}
// }
// Bindings is in the Render interface.
func (c *context) Bindings() map[string]interface{} {
return c.bindings
}
// // Bindings is in the Render interface.
// func (c *context) Bindings() map[string]interface{} {
// return c.bindings
// }

View File

@ -47,19 +47,19 @@ func (e engine) ParseTemplate(text []byte) (Template, error) {
}
// ParseAndRender is in the Engine interface.
func (e engine) ParseAndRender(text []byte, c Context) ([]byte, error) {
func (e engine) ParseAndRender(text []byte, b Bindings) ([]byte, error) {
t, err := e.ParseTemplate(text)
if err != nil {
return nil, err
}
return t.Render(c)
return t.Render(b)
}
// ParseAndRenderString is in the Engine interface.
func (e engine) ParseAndRenderString(text string, c Context) (string, error) {
b, err := e.ParseAndRender([]byte(text), c)
func (e engine) ParseAndRenderString(text string, b Bindings) (string, error) {
bs, err := e.ParseAndRender([]byte(text), b)
if err != nil {
return "", err
}
return string(b), nil
return string(bs), nil
}

View File

@ -30,9 +30,9 @@ type Engine interface {
ParseTemplate([]byte) (Template, error)
// ParseAndRender parses and then renders the template.
ParseAndRender([]byte, Context) ([]byte, error)
ParseAndRender([]byte, Bindings) ([]byte, error)
// ParseAndRenderString is a convenience wrapper for ParseAndRender, that has string input and output.
ParseAndRenderString(string, Context) (string, error)
ParseAndRenderString(string, Bindings) (string, error)
}
// Template renders a template according to scope.
@ -40,19 +40,12 @@ type Engine interface {
// Bindings is a map of liquid variable names to objects.
type Template interface {
// Render executes the template with the specified bindings.
Render(Context) ([]byte, error)
Render(Bindings) ([]byte, error)
// RenderString is a convenience wrapper for Render, that has string input and output.
RenderString(Context) (string, error)
RenderString(Bindings) (string, error)
}
// Context supplies variable bindings and other information to a
// Render.
//
// In the future, it will hold methods to get and set the current
// filename.
type Context interface {
Bindings() map[string]interface{}
}
type Bindings map[string]interface{}
// TagDefinition is the type of a function that parses the argument string "args" from a tag "{% tagname args %}",
// and returns a renderer.

View File

@ -19,19 +19,19 @@ var liquidTests = []struct{ in, expected string }{
{`{{ "upper" | upcase }}`, "UPPER"},
}
var testContext = NewContext(map[string]interface{}{
var testBindings = map[string]interface{}{
"x": 123,
"ar": []string{"first", "second", "third"},
"page": map[string]interface{}{
"title": "Introduction",
},
})
}
func TestLiquid(t *testing.T) {
engine := NewEngine()
for i, test := range liquidTests {
t.Run(fmt.Sprint(i+1), func(t *testing.T) {
out, err := engine.ParseAndRenderString(test.in, testContext)
out, err := engine.ParseAndRenderString(test.in, testBindings)
require.NoErrorf(t, err, test.in)
require.Equalf(t, test.expected, out, test.in)
})
@ -42,7 +42,7 @@ func TestTemplateRenderString(t *testing.T) {
engine := NewEngine()
template, err := engine.ParseTemplate([]byte(`{{ "hello world" | capitalize }}`))
require.NoError(t, err)
out, err := template.RenderString(testContext)
out, err := template.RenderString(testBindings)
require.NoError(t, err)
require.Equal(t, "Hello world", out)
}
@ -55,8 +55,7 @@ func Example() {
"title": "Introduction",
},
}
context := NewContext(bindings)
out, err := engine.ParseAndRenderString(template, context)
out, err := engine.ParseAndRenderString(template, bindings)
if err != nil {
log.Fatalln(err)
}
@ -72,7 +71,7 @@ func Example_filter() {
bindings := map[string]interface{}{
"title": "Introduction",
}
out, err := engine.ParseAndRenderString(template, NewContext(bindings))
out, err := engine.ParseAndRenderString(template, bindings)
if err != nil {
log.Fatalln(err)
}
@ -90,7 +89,7 @@ func Example_tag() {
template := `{% echo hello world %}`
bindings := map[string]interface{}{}
out, err := engine.ParseAndRenderString(template, NewContext(bindings))
out, err := engine.ParseAndRenderString(template, bindings)
if err != nil {
log.Fatalln(err)
}
@ -112,7 +111,7 @@ func Example_tag_pair() {
template := `{% length %}abc{% endlength %}`
bindings := map[string]interface{}{}
out, err := engine.ParseAndRenderString(template, NewContext(bindings))
out, err := engine.ParseAndRenderString(template, bindings)
if err != nil {
log.Fatalln(err)
}

View File

@ -12,9 +12,9 @@ type template struct {
}
// Render executes the template within the bindings environment.
func (t *template) Render(c Context) ([]byte, error) {
func (t *template) Render(b Bindings) ([]byte, error) {
buf := new(bytes.Buffer)
err := t.ast.Render(buf, chunks.NewContext(c.Bindings(), t.settings))
err := t.ast.Render(buf, chunks.NewContext(b, t.settings))
if err != nil {
return nil, err
}
@ -22,10 +22,10 @@ func (t *template) Render(c Context) ([]byte, error) {
}
// RenderString is a convenience wrapper for Render, that has string input and output.
func (t *template) RenderString(c Context) (string, error) {
b, err := t.Render(c)
func (t *template) RenderString(b Bindings) (string, error) {
bs, err := t.Render(b)
if err != nil {
return "", err
}
return string(b), nil
return string(bs), nil
}