How to treat parameters to custom tags as variables?

371 views
Skip to first unread message

Seth Ladd

unread,
Oct 5, 2011, 2:47:46 PM10/5/11
to liquid-t...@googlegroups.com
Hello,

I've written a custom tag and I'd like to be able to pass a variable into the tag. However, Liquid will turn that variable into a string literal.

For example:

{% nav page /some/url.html %} blah blah {% endnav %}

The page is a variable, not a string.

Is there a way to treat page as a variable and not a string in Liquid?

Thanks very much in advance!
Seth

etagwerker

unread,
Oct 10, 2011, 12:15:18 PM10/10/11
to liquid-t...@googlegroups.com
I believe you need to save that string when you initialize the block instance. 

Then later when you want to render the block, you use that string to access a variable in the context. 

Something like this: 

  def initialize(tag_name, markup, tokens)
    if markup =~ Syntax
      @page_name = $1
    else
      raise ::Liquid::SyntaxError.new("Syntax Error!")
    end

    super
  end

  def render(context)
    context.stack do
      collection = context[@page_name]
      ...

    end
  end

I hope it helps. 

- Ernesto
Reply all
Reply to author
Forward
0 new messages