Class: Liquid2::ForTag
- Inherits:
-
Tag
show all
- Defined in:
- lib/liquid2/nodes/tags/for.rb
Overview
Constant Summary
collapse
- END_BLOCK =
Set["endfor", "else"]
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
#partial_scope, #render_with_disabled_tag_check, #template_scope
Constructor Details
#initialize(token, expression, block, default) ⇒ ForTag
Returns a new instance of ForTag.
32
33
34
35
36
37
38
|
# File 'lib/liquid2/nodes/tags/for.rb', line 32
def initialize(token, expression, block, default)
super(token)
@expression = expression
@block = block
@default = default
@blank = block.blank && (!default || default.blank)
end
|
Class Method Details
.parse(token, parser) ⇒ Object
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
# File 'lib/liquid2/nodes/tags/for.rb', line 10
def self.parse(token, parser)
parser.expect_expression
expression = parser.parse_loop_expression
parser.carry_whitespace_control
parser.eat(:token_tag_end)
block = parser.parse_block(END_BLOCK)
if parser.tag?("else")
parser.eat_empty_tag("else")
default = parser.parse_block(END_BLOCK)
else
default = nil
end
parser.eat_empty_tag("endfor")
new(token, expression, block, default)
end
|
Instance Method Details
#block_scope ⇒ Object
77
78
79
|
# File 'lib/liquid2/nodes/tags/for.rb', line 77
def block_scope
[@expression.identifier, Identifier.new([:token_word, "forloop", @expression.token.last])]
end
|
#children(_static_context, include_partials: true) ⇒ Object
68
69
70
71
72
73
|
# File 'lib/liquid2/nodes/tags/for.rb', line 68
def children(_static_context, include_partials: true)
nodes = [@block]
nodes << (@default || raise) if @default
nodes
end
|
#expressions ⇒ Object
75
|
# File 'lib/liquid2/nodes/tags/for.rb', line 75
def expressions = [@expression]
|
#render(context, buffer) ⇒ Object
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
|
# File 'lib/liquid2/nodes/tags/for.rb', line 40
def render(context, buffer)
array = @expression.evaluate(context)
if array.empty?
return @default ? (@default || raise).render(context, buffer) : 0
end
name = @expression.identifier.name
forloop = ForLoop.new(@expression.name, array.length, context.parent_loop(self))
namespace = { "forloop" => forloop }
context.loop(namespace, forloop) do
index = 0
while index < array.length
namespace[name] = array[index]
index += 1
forloop.next
@block.render(context, buffer)
case context.interrupts.pop
when :continue
next
when :break
break
end
end
end
end
|