Class: Liquid::CustomBlocks::KeyIterator

Inherits:
Block
  • Object
show all
Defined in:
lib/liquid/custom_blocks/key_iterator.rb

Instance Method Summary collapse

Constructor Details

#initialize(tag_name, markup, tokens) ⇒ KeyIterator

Returns a new instance of KeyIterator.



4
5
6
7
# File 'lib/liquid/custom_blocks/key_iterator.rb', line 4

def initialize(tag_name, markup, tokens)
  super
  @context_name, @var_name = markup.split(',').map(&:strip)
end

Instance Method Details

#render(context) ⇒ Object



9
10
11
12
13
14
15
16
17
18
# File 'lib/liquid/custom_blocks/key_iterator.rb', line 9

def render(context)
  res = ''
  iterator = context[@context_name].is_a?(Hash) ? context[@context_name].keys : context[@context_name]
  iterator.each.with_index do |key, index|
    context['index'] = index
    context[@var_name] = key
    res += super
  end
  res
end