Added append/prepend filters

This commit is contained in:
Tobias Lütke
2009-01-15 16:59:18 -05:00
parent 88309cf415
commit 13ddc6d96b
2 changed files with 23 additions and 1 deletions
+11 -1
View File
@@ -103,7 +103,17 @@ module Liquid
# remove the first occurrences of a substring # remove the first occurrences of a substring
def remove_first(input, string) def remove_first(input, string)
input.to_s.sub(string, '') input.to_s.sub(string, '')
end end
# add one string to another
def append(input, string)
input.to_s + string.to_s
end
# prepend a string to another
def prepend(input, string)
string.to_s + input.to_s
end
# Add <br /> tags in front of all newlines in input string # Add <br /> tags in front of all newlines in input string
def newline_to_br(input) def newline_to_br(input)
+12
View File
@@ -393,6 +393,18 @@ HERE
assert_template_result('321','{%for item in array reversed %}{{item}}{%endfor%}',assigns) assert_template_result('321','{%for item in array reversed %}{{item}}{%endfor%}',assigns)
end end
def test_append
assigns = {'a' => 'bc', 'b' => 'd' }
assert_template_result('bcd',"{{ a | append: 'd'}}",assigns)
assert_template_result('bcd',"{{ a | append: b}}",assigns)
end
def test_prepend
assigns = {'a' => 'bc', 'b' => 'a' }
assert_template_result('abc',"{{ a | prepend: 'a'}}",assigns)
assert_template_result('abc',"{{ a | prepend: b}}",assigns)
end
def test_ifchanged def test_ifchanged
assigns = {'array' => [ 1, 1, 2, 2, 3, 3] } assigns = {'array' => [ 1, 1, 2, 2, 3, 3] }
assert_template_result('123','{%for item in array%}{%ifchanged%}{{item}}{% endifchanged %}{%endfor%}',assigns) assert_template_result('123','{%for item in array%}{%ifchanged%}{{item}}{% endifchanged %}{%endfor%}',assigns)