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

Using liquid class libraries inside Liquid::Tag #671

Open
vwochnik opened this Issue Nov 18, 2015 · 3 comments

Comments

Projects
None yet
3 participants
@vwochnik

vwochnik commented Nov 18, 2015

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 %}.

@fw42

This comment has been minimized.

Member

fw42 commented Jan 28, 2016

Seems a bit weird to parse things during the render step

@fw42 fw42 added the Question label Jan 28, 2016

@vwochnik

This comment has been minimized.

vwochnik commented Jan 28, 2016

That is true. I could put that part into a parse method but do you suggest I parse inside initialize or is there another possibility?

@pushrax

This comment has been minimized.

Member

pushrax commented Jan 28, 2016

Using initialize is the correct way. Feel free to use Liquid::Parser and Liquid::Expression. This is related to #560 as well, which would make this experience a lot easier.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment