mirror of
https://github.com/Shopify/liquid.git
synced 2026-09-16 17:30:43 -07:00
Add forloop.parentloop as a reference to the parent loop
This commit is contained in:
+12
-8
@@ -42,6 +42,7 @@ module Liquid
|
|||||||
# where 0 is the last item.
|
# where 0 is the last item.
|
||||||
# forloop.first:: Returns true if the item is the first item.
|
# forloop.first:: Returns true if the item is the first item.
|
||||||
# forloop.last:: Returns true if the item is the last item.
|
# forloop.last:: Returns true if the item is the last item.
|
||||||
|
# forloop.parentloop:: Provides access to the parent loop, if present.
|
||||||
#
|
#
|
||||||
class For < Block
|
class For < Block
|
||||||
Syntax = /\A(#{VariableSegment}+)\s+in\s+(#{QuotedFragment}+)\s*(reversed)?/o
|
Syntax = /\A(#{VariableSegment}+)\s+in\s+(#{QuotedFragment}+)\s*(reversed)?/o
|
||||||
@@ -98,18 +99,21 @@ module Liquid
|
|||||||
# Store our progress through the collection for the continue flag
|
# Store our progress through the collection for the continue flag
|
||||||
context.registers[:for][@name] = from + segment.length
|
context.registers[:for][@name] = from + segment.length
|
||||||
|
|
||||||
|
parent_loop = context['forloop'.freeze]
|
||||||
|
|
||||||
context.stack do
|
context.stack do
|
||||||
segment.each_with_index do |item, index|
|
segment.each_with_index do |item, index|
|
||||||
context[@variable_name] = item
|
context[@variable_name] = item
|
||||||
context['forloop'.freeze] = {
|
context['forloop'.freeze] = {
|
||||||
'name'.freeze => @name,
|
'name'.freeze => @name,
|
||||||
'length'.freeze => length,
|
'length'.freeze => length,
|
||||||
'index'.freeze => index + 1,
|
'index'.freeze => index + 1,
|
||||||
'index0'.freeze => index,
|
'index0'.freeze => index,
|
||||||
'rindex'.freeze => length - index,
|
'rindex'.freeze => length - index,
|
||||||
'rindex0'.freeze => length - index - 1,
|
'rindex0'.freeze => length - index - 1,
|
||||||
'first'.freeze => (index == 0),
|
'first'.freeze => (index == 0),
|
||||||
'last'.freeze => (index == length - 1)
|
'last'.freeze => (index == length - 1),
|
||||||
|
'parentloop'.freeze => parent_loop
|
||||||
}
|
}
|
||||||
|
|
||||||
result << @for_block.render(context)
|
result << @for_block.render(context)
|
||||||
|
|||||||
@@ -298,6 +298,22 @@ HERE
|
|||||||
'string' => "test string")
|
'string' => "test string")
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_for_parentloop_references_parent_loop
|
||||||
|
assert_template_result('1.1 1.2 1.3 2.1 2.2 2.3 ',
|
||||||
|
'{% for inner in outer %}{% for k in inner %}' +
|
||||||
|
'{{ forloop.parentloop.index }}.{{ forloop.index }} ' +
|
||||||
|
'{% endfor %}{% endfor %}',
|
||||||
|
'outer' => [[1, 1, 1], [1, 1, 1]])
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_for_parentloop_nil_when_not_present
|
||||||
|
assert_template_result('.1 .2 ',
|
||||||
|
'{% for inner in outer %}' +
|
||||||
|
'{{ forloop.parentloop.index }}.{{ forloop.index }} ' +
|
||||||
|
'{% endfor %}',
|
||||||
|
'outer' => [[1, 1, 1], [1, 1, 1]])
|
||||||
|
end
|
||||||
|
|
||||||
def test_blank_string_not_iterable
|
def test_blank_string_not_iterable
|
||||||
assert_template_result('', "{% for char in characters %}I WILL NOT BE OUTPUT{% endfor %}", 'characters' => '')
|
assert_template_result('', "{% for char in characters %}I WILL NOT BE OUTPUT{% endfor %}", 'characters' => '')
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user