mirror of
https://github.com/Shopify/liquid.git
synced 2026-10-03 09:05:13 -07:00
Merge pull request #577 from Shopify/table-row-blank-string-collection
Fix exception from using an empty string for the table row collection.
This commit is contained in:
@@ -76,9 +76,6 @@ module Liquid
|
|||||||
collection = context.evaluate(@collection_name)
|
collection = context.evaluate(@collection_name)
|
||||||
collection = collection.to_a if collection.is_a?(Range)
|
collection = collection.to_a if collection.is_a?(Range)
|
||||||
|
|
||||||
# Maintains Ruby 1.8.7 String#each behaviour on 1.9
|
|
||||||
return render_else(context) unless iterable?(collection)
|
|
||||||
|
|
||||||
from = if @from == :continue
|
from = if @from == :continue
|
||||||
for_offsets[@name].to_i
|
for_offsets[@name].to_i
|
||||||
else
|
else
|
||||||
@@ -189,10 +186,6 @@ module Liquid
|
|||||||
def render_else(context)
|
def render_else(context)
|
||||||
@else_block ? @else_block.render(context) : ''.freeze
|
@else_block ? @else_block.render(context) : ''.freeze
|
||||||
end
|
end
|
||||||
|
|
||||||
def iterable?(collection)
|
|
||||||
collection.respond_to?(:each) || Utils.non_blank_string?(collection)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
Template.register_tag('for'.freeze, For)
|
Template.register_tag('for'.freeze, For)
|
||||||
|
|||||||
+4
-5
@@ -8,16 +8,15 @@ module Liquid
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.non_blank_string?(collection)
|
|
||||||
collection.is_a?(String) && collection != ''.freeze
|
|
||||||
end
|
|
||||||
|
|
||||||
def self.slice_collection_using_each(collection, from, to)
|
def self.slice_collection_using_each(collection, from, to)
|
||||||
segments = []
|
segments = []
|
||||||
index = 0
|
index = 0
|
||||||
|
|
||||||
# Maintains Ruby 1.8.7 String#each behaviour on 1.9
|
# Maintains Ruby 1.8.7 String#each behaviour on 1.9
|
||||||
return [collection] if non_blank_string?(collection)
|
if collection.is_a?(String)
|
||||||
|
return collection.empty? ? [] : [collection]
|
||||||
|
end
|
||||||
|
return [] unless collection.respond_to?(:each)
|
||||||
|
|
||||||
collection.each do |item|
|
collection.each do |item|
|
||||||
if to && to <= index
|
if to && to <= index
|
||||||
|
|||||||
@@ -57,4 +57,8 @@ class TableRowTest < Minitest::Test
|
|||||||
'{% tablerow n in numbers cols:3 offset:1 limit:6%} {{n}} {% endtablerow %}',
|
'{% tablerow n in numbers cols:3 offset:1 limit:6%} {{n}} {% endtablerow %}',
|
||||||
'numbers' => [0, 1, 2, 3, 4, 5, 6, 7])
|
'numbers' => [0, 1, 2, 3, 4, 5, 6, 7])
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_blank_string_not_iterable
|
||||||
|
assert_template_result("<tr class=\"row1\">\n</tr>\n", "{% tablerow char in characters cols:3 %}I WILL NOT BE OUTPUT{% endtablerow %}", 'characters' => '')
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user