Skip to content
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

Get all variables used in a template #685

Closed
anitsirc opened this issue Jan 21, 2016 · 12 comments
Closed

Get all variables used in a template #685

anitsirc opened this issue Jan 21, 2016 · 12 comments

Comments

@anitsirc
Copy link

Hi,

Is there a way of finding out all variables being used in a template, or at least the ones that it couldn't replace when rendering the template? Both, the ones used as {{ name }} and also the ones used in other tags {% if name == 'John' %}

Thanks :)

@fw42
Copy link
Contributor

fw42 commented Jan 21, 2016

Not that I know of, no. Sorry :(

@fw42 fw42 closed this as completed Jan 21, 2016
@pushrax
Copy link
Contributor

pushrax commented Jan 21, 2016

You could add some hooks to the variable lookup function if you are ok with running a custom liquid version.

@dsignr
Copy link

dsignr commented Feb 28, 2016

Hi, is there a solution for this yet? I'd also like to find all variables used in a template. Thank you.

@fw42
Copy link
Contributor

fw42 commented Feb 28, 2016

No solution, sorry. And nothing planned so far. But I'm open to look at suggestions.

@dsignr
Copy link

dsignr commented Feb 29, 2016

Somewhat hacky, but I put together this for my use case:

def get_variables(t)
      variables = []
      nodes = Liquid::Template.parse(t).root.nodelist
      nodes.each do |n|
        variables << n.name.name if n.is_a? Liquid::Variable
      end #End loop
      #Return
      variables.uniq
 end #End method

>> get_variables("{{friend}} is such a {{animal}}")
>> ["friend", "animal"]

Not even sure if this is ok. What do you think? :D

Edit: (You know what I did)

@pushrax
Copy link
Contributor

pushrax commented Feb 29, 2016

Assuming you only care about top level variables that will work. You also need to check that n.name is a VariableLookup though, if you have something like {{ "foo" }} then it'll be a string.

@dsignr
Copy link

dsignr commented Mar 1, 2016

Wow, I didn't know that you could have variables like that. Thanks for the suggestion, thanks!

@hugopl
Copy link

hugopl commented Apr 6, 2017

If you export all your variables using Liquid::Drop, you can run your liquid template against mock liquid drop with same methods as the usual liquid drop, but registering what variables were used.

@JanStevens
Copy link

With #1025 being available in 4.0.2 this can now be achieved using the following code

Liquid::ParseTreeVisitor.for(Liquid::Template.parse(liquid_string).root).add_callback_for(Liquid::VariableLookup) do |node|
  [node.name, *node.lookups].join('.')
end.visit.flatten.compact

@viral810
Copy link

viral810 commented May 11, 2022

liquid_string = "Hi {{ email }}"

vars = Liquid::Template
.parse(liquid_string)
.root
.nodelist
.select { _1.is_a? Liquid::Variable }
.map(&:name)
.map(&:name)
.uniq

vars # => ["email"]

@fer9305
Copy link

fer9305 commented Nov 28, 2022

With #1025 being available in 4.0.2 this can now be achieved using the following code

Liquid::ParseTreeVisitor.for(Liquid::Template.parse(liquid_string).root).add_callback_for(Liquid::VariableLookup) do |node|
  [node.name, *node.lookups].join('.')
end.visit.flatten.compact

what would be the best way to filter out variables from for loops? Currently if i run this for a template that includes a loop it retrieves the variable used to iterate over the collection, for example:

{% for order in orders %}
  {{ order.id }}
{% endfor %}

as a result I get orders and order.id

@marctorrelles
Copy link

@fer9305 I'm also interested in this. Did you find a way to filter them out?

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

No branches or pull requests

9 participants