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

Template.analyze's representation of variables is lossy #87

Closed
brian-vanderford opened this issue Dec 15, 2022 · 1 comment · Fixed by #88
Closed

Template.analyze's representation of variables is lossy #87

brian-vanderford opened this issue Dec 15, 2022 · 1 comment · Fixed by #88
Labels
bug Something isn't working enhancement New feature or request

Comments

@brian-vanderford
Copy link
Contributor

brian-vanderford commented Dec 15, 2022

>>> from liquid import Environment
>>> env = Environment()      
>>> env.from_string("{{thing['foo.bar']}}").analyze().variables # property "foo.bar" of thing
{'thing.foo.bar': [('<string>', 1)]}
>>> env.from_string("{{thing.foo.bar]}}").analyze().variables   # property bar of property foo of thing
{'thing.foo.bar': [('<string>', 1)]}

thing['foo.bar'] and thing.foo.bar are distinct, but end up with the same representation. It would be helpful to be able to get a more structured representation (maybe with a keyword arg to analyze) so that thing['foo.bar'] is something like ('thing', 'foo.bar') and thing.foo.bar ('thing', 'foo', 'bar').

@jg-rp jg-rp added bug Something isn't working enhancement New feature or request labels Dec 16, 2022
jg-rp added a commit that referenced this issue Dec 16, 2022
@jg-rp jg-rp closed this as completed in #88 Dec 16, 2022
@jg-rp
Copy link
Owner

jg-rp commented Dec 16, 2022

Hi @brian-vanderford,

The string representation of variables has been fixed in version 1.6.0, and those strings now include a parts property, being a tuple representation of the variable. No extra arguments to analyze() required.

from liquid import Environment

env = Environment()
template = env.from_string("{{ thing.foo.bar }} {{ thing['foo.bar'] }}")

for var, location in template.analyze().variables.items():
    for template_name, line_number in location:
        print(f"{var} {var.parts} found in '{template_name}' on line {line_number}")

output

thing.foo.bar ('thing', 'foo', 'bar') found in '<string>' on line 1
thing["foo.bar"] ('thing', 'foo.bar') found in '<string>' on line 1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants