Skip to content

Commit

Permalink
Merge pull request #86 from jamslinger/custom-writer
Browse files Browse the repository at this point in the history
Add FRender to allow rendering into a custom io.Writer
  • Loading branch information
danog authored Apr 26, 2024
2 parents 2b89126 + 59a0a53 commit f9950df
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
9 changes: 9 additions & 0 deletions engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,15 @@ func (e *Engine) ParseAndRender(source []byte, b Bindings) ([]byte, SourceError)
return tpl.Render(b)
}

// ParseAndFRender parses and then renders the template into w.
func (e *Engine) ParseAndFRender(w io.Writer, source []byte, b Bindings) SourceError {
tpl, err := e.ParseTemplate(source)
if err != nil {
return err
}
return tpl.FRender(w, b)
}

// ParseAndRenderString is a convenience wrapper for ParseAndRender, that takes string input and returns a string.
func (e *Engine) ParseAndRenderString(source string, b Bindings) (string, SourceError) {
bs, err := e.ParseAndRender([]byte(source), b)
Expand Down
21 changes: 21 additions & 0 deletions engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"fmt"
"io"
"strings"
"testing"

"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -39,6 +40,26 @@ func TestEngine_ParseAndRenderString(t *testing.T) {
}
}

type capWriter struct {
bytes.Buffer
}

func (c *capWriter) Write(bs []byte) (int, error) {
return c.Buffer.Write([]byte(strings.ToUpper(string(bs))))
}

func TestEngine_ParseAndFRender(t *testing.T) {
engine := NewEngine()
for i, test := range liquidTests {
t.Run(fmt.Sprint(i+1), func(t *testing.T) {
wr := capWriter{}
err := engine.ParseAndFRender(&wr, []byte(test.in), testBindings)
require.NoErrorf(t, err, test.in)
require.Equalf(t, strings.ToUpper(test.expected), wr.String(), test.in)
})
}
}

func TestEngine_ParseAndRenderString_ptr_to_hash(t *testing.T) {
params := map[string]interface{}{
"message": &map[string]interface{}{
Expand Down
10 changes: 10 additions & 0 deletions template.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package liquid

import (
"bytes"
"io"

"github.com/osteele/liquid/parser"
"github.com/osteele/liquid/render"
Expand Down Expand Up @@ -40,6 +41,15 @@ func (t *Template) Render(vars Bindings) ([]byte, SourceError) {
return buf.Bytes(), nil
}

// FRender executes the template with the specified variable bindings and renders it into w.
func (t *Template) FRender(w io.Writer, vars Bindings) SourceError {
err := render.Render(t.root, w, vars, *t.cfg)
if err != nil {
return err
}
return nil
}

// RenderString is a convenience wrapper for Render, that has string input and output.
func (t *Template) RenderString(b Bindings) (string, SourceError) {
bs, err := t.Render(b)
Expand Down

0 comments on commit f9950df

Please sign in to comment.