Class: Liquid::Cycle

Inherits:
Tag
  • Object
show all
Defined in:
lib/liquid/standardtags.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.



45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/liquid/standardtags.rb', line 45

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



59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/liquid/standardtags.rb', line 59

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