From 8319d78c2e926c1cfb30cbc1bb8e85e5b0fa5548 Mon Sep 17 00:00:00 2001 From: Prathan Thananart Date: Thu, 28 Apr 2011 12:00:21 +0700 Subject: [PATCH 1/3] Added a failing test case for for-else --- test/lib/liquid/tags/standard_tag_test.rb | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/test/lib/liquid/tags/standard_tag_test.rb b/test/lib/liquid/tags/standard_tag_test.rb index 4e14949..46c1a7f 100644 --- a/test/lib/liquid/tags/standard_tag_test.rb +++ b/test/lib/liquid/tags/standard_tag_test.rb @@ -102,6 +102,12 @@ HERE assigns) end + def test_for_else + assert_template_result('+++', '{%for item in array%}+{%else%}-{%endfor%}', 'array'=>[1,2,3]) + assert_template_result('-', '{%for item in array%}+{%else%}-{%endfor%}', 'array'=>[]) + assert_template_result('-', '{%for item in array%}+{%else%}-{%endfor%}', 'array'=>nil) + end + def test_limiting assigns = {'array' => [1,2,3,4,5,6,7,8,9,0]} assert_template_result('12', '{%for i in array limit:2 %}{{ i }}{%endfor%}', assigns) From caf59940d34c98d66ca8ae22142a9adca7e7f000 Mon Sep 17 00:00:00 2001 From: Prathan Thananart Date: Thu, 28 Apr 2011 13:07:41 +0700 Subject: [PATCH 2/3] Added code to make for-else work --- lib/liquid/tags/for.rb | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/lib/liquid/tags/for.rb b/lib/liquid/tags/for.rb index b17a332..810eb0f 100644 --- a/lib/liquid/tags/for.rb +++ b/lib/liquid/tags/for.rb @@ -58,8 +58,14 @@ module Liquid raise SyntaxError.new("Syntax Error in 'for loop' - Valid syntax: for [item] in [collection]") end + @nodelist = @for_block = [] super end + + def unknown_tag(tag, markup, tokens) + return super unless tag == 'else' + @nodelist = @else_block = [] + end def render(context) context.registers[:for] ||= Hash.new(0) @@ -67,7 +73,7 @@ module Liquid collection = context[@collection_name] 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' context.registers[:for][@name].to_i @@ -81,7 +87,7 @@ module Liquid segment = slice_collection_using_each(collection, from, to) - return '' if segment.empty? + return render_else(context) if segment.empty? segment.reverse! if @reversed @@ -105,7 +111,7 @@ module Liquid 'first' => (index == 0), 'last' => (index == length - 1) } - result << render_all(@nodelist, context) + result << render_all(@for_block, context) end end result @@ -130,7 +136,14 @@ module Liquid segments end + + private + + def render_else(context) + return @else_block ? [render_all(@else_block, context)] : '' + end + end Template.register_tag('for', For) -end \ No newline at end of file +end From d19213177a552a5980b9db330e0d027e42bbe575 Mon Sep 17 00:00:00 2001 From: Prathan Thananart Date: Thu, 28 Apr 2011 13:11:13 +0700 Subject: [PATCH 3/3] Added documentation for for-else --- lib/liquid/tags/for.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/liquid/tags/for.rb b/lib/liquid/tags/for.rb index 810eb0f..9461a8b 100644 --- a/lib/liquid/tags/for.rb +++ b/lib/liquid/tags/for.rb @@ -13,6 +13,8 @@ module Liquid #
# Item {{ forloop.index }}: {{ item.name }} #
+ # {% else %} + # There is nothing in the collection. # {% endfor %} # # You can also define a limit and offset much like SQL. Remember