Class: Liquid::Cycle
Constant Summary collapse
- SimpleSyntax =
/#{QuotedFragment}/
- NamedSyntax =
/(#{QuotedFragment})\s*\:\s*(.*)/
Instance Attribute Summary
Attributes inherited from Tag
Instance Method Summary collapse
-
#initialize(markup, tokens) ⇒ Cycle
constructor
A new instance of Cycle.
- #render(context) ⇒ Object
Methods inherited from Tag
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 |