Skip to content

Expose the template ast - #59

Merged
osteele merged 1 commit into
osteele:masterfrom
carolynvs:expose-ast
Feb 11, 2022
Merged

Expose the template ast#59
osteele merged 1 commit into
osteele:masterfrom
carolynvs:expose-ast

Conversation

@carolynvs

Copy link
Copy Markdown
Contributor

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.

Checklist

  • I have read the contribution guidelines.
  • make test passes.
  • make lint passes.
  • New and changed code is covered by tests.
  • Performance improvements include benchmarks. N/A
  • Changes match the documented (not just the implemented) behavior of Shopify. N/A

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 <me@carolynvanslyck.com>
@osteele osteele self-assigned this Feb 7, 2022
@osteele
osteele merged commit 2aa750d into osteele:master Feb 11, 2022
@osteele

osteele commented Feb 11, 2022

Copy link
Copy Markdown
Owner

Thank you for your contribution!

Do you have example code where you use this? If so, I would like to add a link to this in the documentation; it sounds like it could be useful material for users of this package.

@carolynvs

Copy link
Copy Markdown
Contributor Author

How about I make a small godoc example and submit a PR so that it's easy to find? We are using it in Porter, so that we can determine which secrets to retrieve from vault and inject back into the config file but I think it would be easier to understand without all the extra Porter stuff going on.

Here's where we load our template and switch to using different delimiters (I'm so glad you support that!)
https://github.com/getporter/porter/blob/9ca4e22d223a20f4620aa67b9436c71dbcfa454a/pkg/config/loader.go#L89-L105

Here's where we parse the ast in the template and grab out any variables used:
https://github.com/getporter/porter/blob/9ca4e22d223a20f4620aa67b9436c71dbcfa454a/pkg/config/loader.go#L117-L139

@carolynvs
carolynvs deleted the expose-ast branch February 11, 2022 15:27
@carolynvs
carolynvs restored the expose-ast branch February 11, 2022 15:27
osteele pushed a commit that referenced this pull request Nov 6, 2025
Updated CHANGELOG.md to include all merged PRs since version 1.3.0 (2020-02-13):

Added features:
- Jekyll Extensions Support (#114)
- Auto-Escape Feature (#111)
- Template Loader (#107)
- BasicEngine (#104)
- JSON Filter (#84)
- Strict Variables Mode (#74)
- Custom Writer Support (#86)
- Template AST Access (#59, #66)
- For-Else Support (#93)
- Unless-Else Support (#68)
- General Range Expressions (#65)
- Loop Modifier Expressions (#67)

Fixes:
- Size filter, slice bounds, division by zero
- Nil pointer handling
- Whitespace control
- Multiline slice
- Block errors
- Map filter with structs
- And more

Also added steve-ky as contributor for ideas/feedback on PR #89.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants