mirror of
https://github.com/Shopify/liquid.git
synced 2026-09-15 08:50:45 -07:00
Convert forloop hash to drop
This commit is contained in:
@@ -48,6 +48,7 @@ require 'liquid/lexer'
|
||||
require 'liquid/parser'
|
||||
require 'liquid/i18n'
|
||||
require 'liquid/drop'
|
||||
require 'liquid/forloop_drop'
|
||||
require 'liquid/extensions'
|
||||
require 'liquid/errors'
|
||||
require 'liquid/interrupts'
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
module Liquid
|
||||
class ForloopDrop < Drop
|
||||
def initialize(name, length, parentloop)
|
||||
@name = name
|
||||
@length = length
|
||||
@parentloop = parentloop
|
||||
@index = 0
|
||||
end
|
||||
|
||||
attr_reader :name, :length, :parentloop
|
||||
|
||||
def index
|
||||
@index + 1
|
||||
end
|
||||
|
||||
def index0
|
||||
@index
|
||||
end
|
||||
|
||||
def rindex
|
||||
@length - @index
|
||||
end
|
||||
|
||||
def rindex0
|
||||
@length - @index - 1
|
||||
end
|
||||
|
||||
def first
|
||||
@index == 0
|
||||
end
|
||||
|
||||
def last
|
||||
@index == @length - 1
|
||||
end
|
||||
|
||||
protected
|
||||
|
||||
def increment!
|
||||
@index += 1
|
||||
end
|
||||
end
|
||||
end
|
||||
+2
-13
@@ -144,11 +144,7 @@ module Liquid
|
||||
result = ''
|
||||
|
||||
context.stack do
|
||||
loop_vars = {
|
||||
'name'.freeze => @name,
|
||||
'length'.freeze => length,
|
||||
'parentloop'.freeze => for_stack[-1]
|
||||
}
|
||||
loop_vars = Liquid::ForloopDrop.new(@name, length, for_stack[-1])
|
||||
|
||||
for_stack.push(loop_vars)
|
||||
|
||||
@@ -157,15 +153,8 @@ module Liquid
|
||||
|
||||
segment.each_with_index do |item, index|
|
||||
context[@variable_name] = item
|
||||
|
||||
loop_vars['index'.freeze] = index + 1
|
||||
loop_vars['index0'.freeze] = index
|
||||
loop_vars['rindex'.freeze] = length - index
|
||||
loop_vars['rindex0'.freeze] = length - index - 1
|
||||
loop_vars['first'.freeze] = (index == 0)
|
||||
loop_vars['last'.freeze] = (index == length - 1)
|
||||
|
||||
result << @for_block.render(context)
|
||||
loop_vars.send(:increment!)
|
||||
|
||||
# Handle any interrupts if they exist.
|
||||
if context.interrupt?
|
||||
|
||||
Reference in New Issue
Block a user