Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to parse all outputs from template string? #524

Closed
timsofteng opened this issue Jul 22, 2022 · 2 comments
Closed

How to parse all outputs from template string? #524

timsofteng opened this issue Jul 22, 2022 · 2 comments

Comments

@timsofteng
Copy link

timsofteng commented Jul 22, 2022

Hello.
I need to parse all outputs from template.

{{bucket}}
{% for product in products %}
   {{ product.name }} ads
{% endfor %}

So I want to get array like ["bucket", "product.name"] or something like that.
How can I do it?

@harttle
Copy link
Owner

harttle commented Jul 23, 2022

There is a way, but now there's no AST definition, that means the way to access the parsed tokens is implementation specific and subject to change. For example the following code outputs what you mentioned above:

const { Liquid } = require('liquidjs')
const engine = new Liquid()
const tpls = engine.parse(`
{{bucket}}
{% for product in products %}
   {{ product.name }} ads
{% endfor %}
`)
console.log(tpls[1].token.content) // outputs: bucket
console.log(tpls[3].impl.templates[1].token.content) // outputs: product.name

Note that the name .impl is defined in for implementation, and the way to access children nodes can be different for each tag. And tpls[0] is represents \n before the first output {{bucket}}.

I'm planning to provide a well-defined AST in the next major version (latter this year). Just created this work item to remind me: https://github.com/harttle/liquidjs/projects/1#card-84455385

@timsofteng
Copy link
Author

Hm. I still don't understand how can I know specific indexes of array's elements? Imagine I have big complex template. How can I grab outputs?

Repository owner locked and limited conversation to collaborators Aug 14, 2022
@harttle harttle converted this issue into discussion #528 Aug 14, 2022

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Projects
None yet
Development

No branches or pull requests

2 participants