Shopify / liquid

Liquid markup language. Safe, customer facing template language for flexible web apps.

Home Page:https://shopify.github.io/liquid/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Expose Liquid::Condition in public API

jscheid opened this issue · comments

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?