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

Optionally allow shorthand indexes in variable paths #165

Merged
merged 3 commits into from
Feb 8, 2025
Merged

Conversation

jg-rp
Copy link
Owner

@jg-rp jg-rp commented Feb 8, 2025

This PR adds a shorthand_indexes class variable to liquid.Environment. When shorthand_indexes is set to True (default is False), array indexes in variable paths need not be surrounded by square brackets.

Any custom tags that create instances of liquid.expressions.TokenStream should pass shorthand_indexes=self.env.shorthand_indexes when constructing the stream.

In a future release we'll refactor expression parsing to make sure the current environment is available to all expression parsing functions/methods so we don't need to pass a shorthand_indexes argument in a million places.

See #39.

from liquid import Environment

class MyEnv(Environment):
    shorthand_indexes = True

env = MyEnv()

data = {"foo": ["World", "Liquid"]}
template = env.from_string("Hello, {{ foo.0 }}!")
print(template) # Hello, World!

TODO:

  • Docs
  • Common expression parser
  • Macro and call arguments

@jg-rp jg-rp merged commit a4b1a8f into main Feb 8, 2025
40 checks passed
@jg-rp jg-rp deleted the shorthand-indexes branch February 8, 2025 10:21
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.

1 participant