Class: Ruty::Tags::Cycle

Inherits:
Ruty::Tag show all
Defined in:
lib/ruty/tags/looptools.rb

Overview

A tag that cycles through a list of values each iteration Useful for example if you want alternating rows:

{% for row in rows %}
  <tr class="{% cycle 'row1', 'row2' %}">
    <td>..</td>
  </tr>
{% endfor %}

Instance Method Summary collapse

Constructor Details

#initialize(parser, argstring) ⇒ Cycle

Returns a new instance of Cycle.


19
20
21
22
23
# File 'lib/ruty/tags/looptools.rb', line 19

def initialize parser, argstring
  args = parser.parse_arguments(argstring)
  parser.fail('At least one item is required for cycle') if args.empty?
  @items = args
end

Instance Method Details

#render_node(context, stream) ⇒ Object


25
26
27
28
29
30
# File 'lib/ruty/tags/looptools.rb', line 25

def render_node context, stream
  item = @items[context[self] = ((context[self] || -1) + 1) % @items.length]
  item = context.resolve(item) if item.is_a?(Symbol)
  stream << item.to_s
  nil
end