public
Rubygem
Description: Liquid markup language. Save, customer facing template language for flexible web apps.
Homepage: http://www.liquidmarkup.org
Clone URL: git://github.com/tobi/liquid.git
Click here to lend your support to: liquid and make a donation at www.pledgie.com !
Implemented reversed flag on for loops {% for a in b reversed %}
Tobias Lütke (author)
Thu May 08 14:17:41 -0700 2008
commit  63f9a05223070337021164eabc06059bfafd20d9
tree    681e5d7e9d1dca228008033dc911c5f3cc6a0b94
parent  8f45647aa37d784163980e9e23e4e515d5ecfd70
...
20
21
22
23
 
 
 
24
25
26
...
40
41
42
43
 
44
45
46
47
48
49
 
 
50
51
52
...
80
81
82
 
 
83
84
85
86
 
 
87
88
89
...
20
21
22
 
23
24
25
26
27
28
...
42
43
44
 
45
46
47
48
49
50
 
51
52
53
54
55
...
83
84
85
86
87
88
89
 
 
90
91
92
93
94
0
@@ -20,7 +20,9 @@ module Liquid
0
   #
0
   # {% for item in collection limit:5 offset:10 %}
0
   # {{ item.name }}
0
- # {% end %}
0
+ # {% end %}
0
+ #
0
+ # To reverse the for loop simply use {% for item in collection reversed %}
0
   #
0
   # == Available variables:
0
   #
0
@@ -40,13 +42,14 @@ module Liquid
0
   # forloop.last:: Returns true if the item is the last item.
0
   #
0
   class For < Block
0
- Syntax = /(\w+)\s+in\s+(#{VariableSignature}+)/
0
+ Syntax = /(\w+)\s+in\s+(#{VariableSignature}+)\s*(reversed)?/
0
   
0
     def initialize(tag_name, markup, tokens)
0
       if markup =~ Syntax
0
         @variable_name = $1
0
         @collection_name = $2
0
- @name = "#{$1}-#{$2}"
0
+ @name = "#{$1}-#{$2}"
0
+ @reversed = $3
0
         @attributes = {}
0
         markup.scan(TagAttributes) do |key, value|
0
           @attributes[key] = value
0
@@ -80,10 +83,12 @@ module Liquid
0
       
0
       return '' if segment.empty?
0
       
0
+ segment.reverse! if @reversed
0
+
0
       result = []
0
         
0
- length = segment.length
0
-
0
+ length = segment.length
0
+
0
       # Store our progress through the collection for the continue flag
0
       context.registers[:for][@name] = from + segment.length
0
               
...
376
377
378
 
 
 
 
 
379
380
381
...
376
377
378
379
380
381
382
383
384
385
386
0
@@ -376,6 +376,11 @@ HERE
0
     assert_template_result('', '{% if null == true %}?{% endif %}', {})
0
   end
0
   
0
+ def test_for_reversed
0
+ assigns = {'array' => [ 1, 2, 3] }
0
+ assert_template_result('321','{%for item in array reversed %}{{item}}{%endfor%}',assigns)
0
+ end
0
+
0
   def test_ifchanged
0
     assigns = {'array' => [ 1, 1, 2, 2, 3, 3] }
0
     assert_template_result('123','{%for item in array%}{%ifchanged%}{{item}}{% endifchanged %}{%endfor%}',assigns)

Comments

    No one has commented yet.