2

I am trying to build custom block using the Liquid Templating. Following is my code:

module MyModule
    module Blocks
        class MyBlock < Liquid::Block

            def initialize(tag, markup, tokens)
                super
                @tag = tag
                @tokens = tokens
                @markup = markup
            end

            def render(context)
                p @tokens
            end

        end
    end
end

Liquid::Template.register_tag('myblock', MyModule::Blocks::MyBlock)

And in my template, I have the following code:

{% for i in mypages %}
    {% myblock %} {{ i.title }} {% endmyblock %}
{% endfor %}

My question is how do I get all the content that is passed between the myblock tags. i.e., how do I make i.title available to myblock's render function. I thought tokens captures this, but when I puts tokens it outputs []

Thank You

swaroopsm
  • 1,389
  • 4
  • 18
  • 34
  • 1
    I've not got the answer here ,but in terms of debugging it, extract `{% myblock %}....` out of the `for` loop and put it straight into the post/page, with a static value: `{% myblock %} this is my block {% endmyblock %}` . This way you know what you should expect for sure. – tamouse Oct 28 '13 at 07:04

1 Answers1

1

The render method of Liquid::Block returns the text between the begin and end tags. So just change your render method to this:

def render(context)
  p super
end
Nathan Grigg
  • 724
  • 5
  • 11