mirror of
https://github.com/Shopify/liquid.git
synced 2026-09-16 17:30:43 -07:00
Merge pull request #562 from Shopify/use-find_variable-for-parentloop
Use custom stack for forloop references
This commit is contained in:
+13
-5
@@ -67,7 +67,8 @@ module Liquid
|
||||
end
|
||||
|
||||
def render(context)
|
||||
context.registers[:for] ||= Hash.new(0)
|
||||
for_offsets = context.registers[:for] ||= Hash.new(0)
|
||||
for_stack = context.registers[:for_stack] ||= []
|
||||
|
||||
collection = context.evaluate(@collection_name)
|
||||
collection = collection.to_a if collection.is_a?(Range)
|
||||
@@ -76,7 +77,7 @@ module Liquid
|
||||
return render_else(context) unless iterable?(collection)
|
||||
|
||||
from = if @from == :continue
|
||||
context.registers[:for][@name].to_i
|
||||
for_offsets[@name].to_i
|
||||
else
|
||||
context.evaluate(@from).to_i
|
||||
end
|
||||
@@ -95,14 +96,15 @@ module Liquid
|
||||
length = segment.length
|
||||
|
||||
# Store our progress through the collection for the continue flag
|
||||
context.registers[:for][@name] = from + segment.length
|
||||
for_offsets[@name] = from + segment.length
|
||||
|
||||
parent_loop = context['forloop'.freeze]
|
||||
parent_loop = for_stack.last
|
||||
for_stack.push(nil)
|
||||
|
||||
context.stack do
|
||||
segment.each_with_index do |item, index|
|
||||
context[@variable_name] = item
|
||||
context['forloop'.freeze] = {
|
||||
loop_vars = {
|
||||
'name'.freeze => @name,
|
||||
'length'.freeze => length,
|
||||
'index'.freeze => index + 1,
|
||||
@@ -114,6 +116,9 @@ module Liquid
|
||||
'parentloop'.freeze => parent_loop
|
||||
}
|
||||
|
||||
context['forloop'.freeze] = loop_vars
|
||||
for_stack[-1] = loop_vars
|
||||
|
||||
result << @for_block.render(context)
|
||||
|
||||
# Handle any interrupts if they exist.
|
||||
@@ -124,7 +129,10 @@ module Liquid
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
result
|
||||
ensure
|
||||
for_stack.pop
|
||||
end
|
||||
|
||||
protected
|
||||
|
||||
Reference in New Issue
Block a user