mirror of
https://github.com/Shopify/liquid.git
synced 2026-10-04 01:25:14 -07:00
Added code to make for-else work
This commit is contained in:
+16
-3
@@ -58,16 +58,22 @@ module Liquid
|
|||||||
raise SyntaxError.new("Syntax Error in 'for loop' - Valid syntax: for [item] in [collection]")
|
raise SyntaxError.new("Syntax Error in 'for loop' - Valid syntax: for [item] in [collection]")
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@nodelist = @for_block = []
|
||||||
super
|
super
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def unknown_tag(tag, markup, tokens)
|
||||||
|
return super unless tag == 'else'
|
||||||
|
@nodelist = @else_block = []
|
||||||
|
end
|
||||||
|
|
||||||
def render(context)
|
def render(context)
|
||||||
context.registers[:for] ||= Hash.new(0)
|
context.registers[:for] ||= Hash.new(0)
|
||||||
|
|
||||||
collection = context[@collection_name]
|
collection = context[@collection_name]
|
||||||
collection = collection.to_a if collection.is_a?(Range)
|
collection = collection.to_a if collection.is_a?(Range)
|
||||||
|
|
||||||
return '' unless collection.respond_to?(:each)
|
return render_else(context) unless collection.respond_to?(:each)
|
||||||
|
|
||||||
from = if @attributes['offset'] == 'continue'
|
from = if @attributes['offset'] == 'continue'
|
||||||
context.registers[:for][@name].to_i
|
context.registers[:for][@name].to_i
|
||||||
@@ -81,7 +87,7 @@ module Liquid
|
|||||||
|
|
||||||
segment = slice_collection_using_each(collection, from, to)
|
segment = slice_collection_using_each(collection, from, to)
|
||||||
|
|
||||||
return '' if segment.empty?
|
return render_else(context) if segment.empty?
|
||||||
|
|
||||||
segment.reverse! if @reversed
|
segment.reverse! if @reversed
|
||||||
|
|
||||||
@@ -105,7 +111,7 @@ module Liquid
|
|||||||
'first' => (index == 0),
|
'first' => (index == 0),
|
||||||
'last' => (index == length - 1) }
|
'last' => (index == length - 1) }
|
||||||
|
|
||||||
result << render_all(@nodelist, context)
|
result << render_all(@for_block, context)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
result
|
result
|
||||||
@@ -130,6 +136,13 @@ module Liquid
|
|||||||
|
|
||||||
segments
|
segments
|
||||||
end
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def render_else(context)
|
||||||
|
return @else_block ? [render_all(@else_block, context)] : ''
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
Template.register_tag('for', For)
|
Template.register_tag('for', For)
|
||||||
|
|||||||
Reference in New Issue
Block a user