Add forloop to render for syntax

This commit is contained in:
Mike Angell
2019-10-14 17:24:34 +10:00
parent 1223444738
commit 1c3dcb0ddc
2 changed files with 18 additions and 2 deletions
+9 -2
View File
@@ -43,19 +43,26 @@ module Liquid
context_variable_name = @alias_name || template_name.split('/').last
render_partial_func = ->(var) {
render_partial_func = ->(var, forloop) {
inner_context = context.new_isolated_subcontext
inner_context.template_name = template_name
inner_context.partial = true
inner_context['forloop'] = forloop if forloop
@attributes.each do |key, value|
inner_context[key] = context.evaluate(value)
end
inner_context[context_variable_name] = var unless var.nil?
partial.render_to_output_buffer(inner_context, output)
forloop&.send(:increment!) if forloop
}
variable = @variable_name_expr ? context.evaluate(@variable_name_expr) : nil
variable.is_a?(Array) ? variable.each(&render_partial_func) : render_partial_func.call(variable)
if variable.is_a?(Array)
forloop = Liquid::ForloopDrop.new(template_name, variable.count, nil)
variable.each { |var| render_partial_func.call(var, forloop) }
else
render_partial_func.call(variable, nil)
end
output
end