liquid icon indicating copy to clipboard operation
liquid copied to clipboard

Expose Liquid::Condition in public API

Open jscheid opened this issue 1 year ago • 0 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?

jscheid avatar Feb 05 '24 12:02 jscheid