Class: Liquid::Cycle

Inherits:
Tag
  • Object
show all
Defined in:
lib/liquid/tags/cycle.rb

Constant Summary collapse

SimpleSyntax =
/#{QuotedFragment}/
NamedSyntax =
/(#{QuotedFragment})\s*\:\s*(.*)/

Instance Attribute Summary

Attributes inherited from Tag

#nodelist

Instance Method Summary collapse

Methods inherited from Tag

#name, #parse

Constructor Details

#initialize(markup, tokens) ⇒ Cycle

Returns a new instance of Cycle.



6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/liquid/tags/cycle.rb', line 6

def initialize(markup, tokens)
  case markup
  when NamedSyntax
  	@variables = variables_from_string($2)
  	@name = $1
  when SimpleSyntax
    @variables = variables_from_string(markup)
  	@name = "'#{@variables.to_s}'"
  else
    raise SyntaxError.new("Syntax Error in 'cycle' - Valid syntax: cycle [name :] var [, var2, var3 ...]")
  end

end

Instance Method Details

#render(context) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/liquid/tags/cycle.rb', line 20

def render(context)
  context.registers[:cycle] ||= Hash.new(0)

  context.stack do
    key = context[@name]	
    iteration = context.registers[:cycle][key]
    result = context[@variables[iteration]]
    iteration += 1    
    iteration  = 0  if iteration >= @variables.size 
    context.registers[:cycle][key] = iteration
    result 
  end
end