Class: Liquify::Block

Inherits:
Liquid::Block
  • Object
show all
Defined in:
lib/liquify/block.rb

Overview

Creating a block with Liquify is as simple as creating a Ruby class.

class FooBlock < Liquify::Block
  def bar(params)
   ...
  end

  def baz
   ...
  end
end 

Liquify.setup do |config|
  config.register_tag :foo_block, FooBlock
end

{% foo_block as: 'f' %}
  {{ f.bar 'arg1', key: 'value' }}
  {{ f.baz }}
{% endfoo_block %}

If you need wrapper content override the invoke method and call yield within it.

Defined Under Namespace

Classes: Drop

Instance Method Summary collapse

Constructor Details

#initialize(tag_name, markup, tokens) ⇒ Block

:nodoc:



28
29
30
31
# File 'lib/liquify/block.rb', line 28

def initialize(tag_name, markup, tokens) # :nodoc:
  @tokens = tokens.dup
  super(tag_name, markup, tokens)
end

Instance Method Details

#render(context) ⇒ Object

:nodoc:



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/liquify/block.rb', line 33

def render(context) # :nodoc:
  params = Liquify::Parameter.new(@markup, context)
  options = params.dup.extract_options!
  drop_tokens = @tokens.grep /\{\{\s*#{options['as']}\..*\}\}/

  context[options['as']] = Liquify::Block::Drop.new(self, drop_tokens, context)

  args = []
  args << params if method(:invoke).arity == 1

  context.stack do
    invoke(*args) do
      render_all(@nodelist, context).join
    end
  end
end