Skip to content

Commit

Permalink
Expose the template ast
Browse files Browse the repository at this point in the history
Sometimes a consumer of a template needs to know what objects were used.
In my case, a template can reference secret values from a secret store
vault and instead of passing all possible secrets to the template only
to render two of them, we use the ast to determine which are used and
only retrieve those values from the vault before rendering the template.

Exposing the ast allows us to use the liquid APIs just like normal,
without having to jump through hoops to build the ast ourselves using
the other types exported in this library.

Signed-off-by: Carolyn Van Slyck <[email protected]>
  • Loading branch information
carolynvs committed Jan 31, 2022
1 parent 5cbe290 commit 2e107be
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
6 changes: 6 additions & 0 deletions template.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ func newTemplate(cfg *render.Config, source []byte, path string, line int) (*Tem
return &Template{root, cfg}, nil
}

// GetRoot returns the root node of the abstract syntax tree (AST) representing
// the parsed template.
func (t *Template) GetRoot() render.Node {
return t.root
}

// Render executes the template with the specified variable bindings.
func (t *Template) Render(vars Bindings) ([]byte, SourceError) {
buf := new(bytes.Buffer)
Expand Down
6 changes: 6 additions & 0 deletions template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ import (
"github.com/stretchr/testify/require"
)

func TestTemplate_GetRoot(t *testing.T) {
root := &render.SeqNode{}
tmpl := Template{root: root}
require.Same(t, root, tmpl.GetRoot())
}

func TestTemplate_RenderString(t *testing.T) {
engine := NewEngine()
tpl, err := engine.ParseTemplate([]byte(`{{ "hello world" | capitalize }}`))
Expand Down

0 comments on commit 2e107be

Please sign in to comment.