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

Expose Liquid::Condition in public API #1785

Open
jscheid opened this issue Feb 5, 2024 · 0 comments
Open

Expose Liquid::Condition in public API #1785

jscheid opened this issue Feb 5, 2024 · 0 comments

Comments

@jscheid
Copy link

jscheid commented Feb 5, 2024

It would be useful to be able to use Liquid conditions outside of templating. This can be hacked by using private APIs like so:

cond = Liquid::If
  .send(
    :new,
    'if',
    condition_str,
    parse_context
  )
  .blocks
  .first or raise
cond.evaluate(context)

...or by abusing templating like so:

tmpl = Liquid::Template.parse(
  "{% if #{condition} %}true{% else %}false{% endif %}",
  parse_context
)
tmpl.render(context) == "true"

The former is hacky and prone to breakage in future releases, the latter is inefficient and has quoting issues. Would it be conceivable to expose this feature in the public API?

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

1 participant