Skip to content

Using liquid class libraries inside Liquid::Tag #671

@vwochnik

Description

@vwochnik

I've written a liquid tag that makes use of Liquid::Parser and Liquid::Expression although not on the official documentation page. I just wanted to check back and ask if that is reasonably sustainable or the code is going to change soon.

Here's a code excerpt:

class ExampleTag < Liquid::Tag
  def initialize(tag_name, markup, tokens)
    super
    @markup = markup
  end

  def render(context)
    p = Liquid::Parser.new(@markup)
    name = Liquid::Expression.parse(exp = p.expression)
    key = context.evaluate(name)
    raise Liquid::SyntaxError.new("Invalid example tag expression: #{exp}") if key.nil?

    # do something with evaluated key

    # loop through arguments, optionally
    if p.consume?(:colon)
      loop do
        arg = Liquid::Expression.parse(exp = p.expression)
        argstr = context.evaluate(arg)
        raise Liquid::SyntaxError.new("Invalid parameter expression: #{exp}") if argstr.nil?
        # do something with parameter
        break if !p.consume?(:comma)
      end
    end

    # do something more, return
  end
end

At this point, I'm not an expert of liquid. What I want to achieve is parametrical expression parsing within a tag. So for example {% xt 'mandatory expression': 'arg1', 'arg2', variable3 %}.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions