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 !
if blocks work with filtered variables
jamesmacaulay (author)
Wed Oct 15 12:06:58 -0700 2008
commit  282786d7e2deb728f82977d4db38b0f7a05e7e76
tree    8c7ff79034b1515cafa9ba201a3952a04a3a8f57
parent  69bc84b7774f7404067c1d553535f61a462d7313
...
22
23
24
25
 
26
27
28
...
33
34
35
36
 
 
 
 
 
 
37
38
39
...
22
23
24
 
25
26
27
28
...
33
34
35
 
36
37
38
39
40
41
42
43
44
0
@@ -22,7 +22,7 @@
0
 $LOAD_PATH.unshift(File.dirname(__FILE__))
0
 
0
 module Liquid
0
-  FilterSperator              = /\|/
0
+  FilterSeparator              = /\|/
0
   ArgumentSeparator           = ','
0
   FilterArgumentSeparator     = ':'
0
   VariableAttributeSeparator  = '.'
0
@@ -33,7 +33,12 @@ module Liquid
0
   VariableStart               = /\{\{/
0
   VariableEnd                 = /\}\}/
0
   VariableIncompleteEnd       = /\}\}?/
0
-  QuotedFragment              = /"[^"]+"|'[^']+'|[^\s,|]+/
0
+  QuotedFragment              = /"[^"]+"|'[^']+'|[^\s,\|]+/
0
+  StrictQuotedFragment        = /"[^"]+"|'[^']+'|[^\s,\|,\:,\,]+/
0
+  FirstFilterArgument         = /#{FilterArgumentSeparator}(?:#{StrictQuotedFragment})/
0
+  OtherFilterArgument         = /#{ArgumentSeparator}(?:#{StrictQuotedFragment})/
0
+  SpacelessFilter             = /#{FilterSeparator}(?:#{StrictQuotedFragment})(?:#{FirstFilterArgument}(?:#{OtherFilterArgument})*)?/
0
+  Expression                  = /(?:#{QuotedFragment}(?:#{SpacelessFilter})*)/
0
   TagAttributes               = /(\w+)\s*\:\s*(#{QuotedFragment})/
0
   TemplateParser              = /(#{TagStart}.*?#{TagEnd}|#{VariableStart}.*?#{VariableIncompleteEnd})/
0
   VariableParser              = /\[[^\]]+\]|#{VariableSegment}+/
...
133
134
135
 
 
 
136
137
138
...
221
222
223
 
 
 
 
224
225
...
133
134
135
136
137
138
139
140
141
...
224
225
226
227
228
229
230
231
232
0
@@ -133,6 +133,9 @@ module Liquid
0
         :blank?
0
       when 'empty'
0
         :empty?
0
+      # filtered variables
0
+      when SpacelessFilter
0
+        filtered_variable(key)
0
       # Single quoted strings
0
       when /^'(.*)'$/
0
         $1.to_s
0
@@ -221,5 +224,9 @@ module Liquid
0
 
0
       object
0
     end
0
+    
0
+    def filtered_variable(markup)
0
+      Variable.new(markup).render(self)
0
+    end
0
   end
0
 end
...
13
14
15
16
 
17
18
19
...
13
14
15
 
16
17
18
19
0
@@ -13,7 +13,7 @@ module Liquid
0
   #
0
   class If < Block
0
     SyntaxHelp = "Syntax Error in tag 'if' - Valid syntax: if [expression]"
0
-    Syntax = /(#{QuotedFragment})\s*([=!<>a-z_]+)?\s*(#{QuotedFragment})?/
0
+    Syntax = /(#{Expression})\s*([=!<>a-z_]+)?\s*(#{Expression})?/
0
     
0
     def initialize(tag_name, markup, tokens)    
0
     
...
19
20
21
22
23
 
 
24
25
26
...
19
20
21
 
 
22
23
24
25
26
0
@@ -19,8 +19,8 @@ module Liquid
0
       @filters = []
0
       if match = markup.match(/\s*(#{QuotedFragment})/)
0
         @name = match[1]
0
-        if markup.match(/#{FilterSperator}\s*(.*)/)
0
-          filters = Regexp.last_match(1).split(/#{FilterSperator}/)
0
+        if markup.match(/#{FilterSeparator}\s*(.*)/)
0
+          filters = Regexp.last_match(1).split(/#{FilterSeparator}/)
0
         
0
           filters.each do |f|    
0
             if matches = f.match(/\s*(\w+)/)
...
112
113
114
 
 
 
 
 
 
 
115
116
117
...
112
113
114
115
116
117
118
119
120
121
122
123
124
0
@@ -112,6 +112,13 @@ class IfElseTest < Test::Unit::TestCase
0
     assert_template_result('elsif','{% if false %}if{% elsif true %}elsif{% endif %}')    
0
   end
0
   
0
+  def test_with_filtered_expressions
0
+    assert_template_result('yes','{% if "BLAH"|downcase == "blah" %}yes{% endif %}')
0
+    assert_template_result('yes','{% if "FOO BAR"|truncatewords:1,"--" == "FOO--" %}yes{% endif %}')
0
+    assert_template_result('yes','{% if "FOO BAR"|truncatewords:1,"--"|downcase == "foo--" %}yes{% endif %}')
0
+    assert_template_result('yes','{% if "foo--" == "FOO BAR"|truncatewords:1,"--"|downcase %}yes{% endif %}')
0
+  end
0
+  
0
   def test_syntax_error_no_variable
0
     assert_raise(SyntaxError){ assert_template_result('', '{% if jerry == 1 %}')}
0
   end

Comments