Class: Liquid2::CycleTag

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

Overview

The standard cycle tag.

Instance Attribute Summary

Attributes inherited from Node

#blank, #token

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Tag

#name

Methods inherited from Node

#block_scope, #children, #partial_scope, #render_with_disabled_tag_check, #template_scope

Constructor Details

#initialize(token, name, items, named) ⇒ CycleTag

Returns a new instance of CycleTag.

Parameters:



36
37
38
39
40
41
42
43
44
# File 'lib/liquid2/nodes/tags/cycle.rb', line 36

def initialize(token, name, items, named)
  super(token)
  @name = name
  @items = items
  @named = named
  @blank = false
  # Generate the cycle key here once if we don't have a potentially dynamic name.
  @key = @items.to_s unless @named
end

Class Method Details

.parse(token, parser) ⇒ CycleTag

Parameters:

Returns:



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/liquid2/nodes/tags/cycle.rb', line 10

def self.parse(token, parser)
  items = [] # : Array[untyped]
  group_name = nil # : untyped?
  named = false
  first = parser.parse_primary

  # Is the first expression followed by a colon? If so, it is a group name
  # followed by items to cycle.
  if parser.current_kind == :token_colon
    group_name = first
    named = true
    parser.next
  else
    items << first
  end

  parser.next if parser.current_kind == :token_comma

  items.push(*parser.parse_positional_arguments)
  parser.carry_whitespace_control
  parser.eat(:token_tag_end)
  new(token, group_name, items, named)
end

Instance Method Details

#expressionsObject



56
# File 'lib/liquid2/nodes/tags/cycle.rb', line 56

def expressions = @items

#render(context, buffer) ⇒ Object



46
47
48
49
50
51
52
53
54
# File 'lib/liquid2/nodes/tags/cycle.rb', line 46

def render(context, buffer)
  key = @key || context.evaluate(@name).to_s
  index = context.tag_namespace[:cycles][key]
  buffer << Liquid2.to_output_s(context.evaluate(@items[index]))

  index += 1
  index = 0 if index >= @items.length
  context.tag_namespace[:cycles][key] = index
end