diff --git a/lib/liquid/tags/for.rb b/lib/liquid/tags/for.rb index 214253a..fd6c3d9 100644 --- a/lib/liquid/tags/for.rb +++ b/lib/liquid/tags/for.rb @@ -76,7 +76,7 @@ module Liquid 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 collection.respond_to?(:each) or collection.is_a?(String) + return render_else(context) unless iterable?(collection) from = if @attributes['offset'] == 'continue' context.registers[:for][@name].to_i @@ -150,6 +150,9 @@ module Liquid return @else_block ? [render_all(@else_block, context)] : '' end + def iterable?(collection) + collection.respond_to?(:each) || (collection.is_a?(String) && collection != '') + end end Template.register_tag('for', For) diff --git a/test/liquid/tags/for_tag_test.rb b/test/liquid/tags/for_tag_test.rb index 7d9ad60..485701d 100644 --- a/test/liquid/tags/for_tag_test.rb +++ b/test/liquid/tags/for_tag_test.rb @@ -195,4 +195,8 @@ HERE '{{val}}{%endfor%}', 'string' => "test string") end + + def test_blank_string_not_iterable + assert_template_result('', "{% for char in characters %}I WILL NOT BE OUTPUT{% endfor %}", 'characters' => '') + end end