mirror of
https://github.com/Shopify/liquid.git
synced 2026-09-12 23:40:45 -07:00
Added filters for basic arithmetic
This commit is contained in:
@@ -155,6 +155,26 @@ module Liquid
|
|||||||
array.last if array.respond_to?(:last)
|
array.last if array.respond_to?(:last)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# addition
|
||||||
|
def plus(input, operand)
|
||||||
|
input + operand if input.respond_to?('+')
|
||||||
|
end
|
||||||
|
|
||||||
|
# subtraction
|
||||||
|
def minus(input, operand)
|
||||||
|
input - operand if input.respond_to?('-')
|
||||||
|
end
|
||||||
|
|
||||||
|
# multiplication
|
||||||
|
def times(input, operand)
|
||||||
|
input * operand if input.respond_to?('*')
|
||||||
|
end
|
||||||
|
|
||||||
|
# division
|
||||||
|
def divided_by(input, operand)
|
||||||
|
input / operand if input.respond_to?('/')
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
Template.register_filter(StandardFilters)
|
Template.register_filter(StandardFilters)
|
||||||
|
|||||||
@@ -118,9 +118,25 @@ class StandardFiltersTest < Test::Unit::TestCase
|
|||||||
assert_template_result "a<br />\nb<br />\nc", "{{ source | newline_to_br }}", 'source' => "a\nb\nc"
|
assert_template_result "a<br />\nb<br />\nc", "{{ source | newline_to_br }}", 'source' => "a\nb\nc"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_plus
|
||||||
|
assert_template_result "2", "{{ 1 | plus:1 }}"
|
||||||
|
assert_template_result "11", "{{ '1' | plus:'1' }}"
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_minus
|
||||||
|
assert_template_result "4", "{{ input | minus:operand }}", 'input' => 5, 'operand' => 1
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_times
|
||||||
|
assert_template_result "12", "{{ 3 | times:4 }}"
|
||||||
|
assert_template_result "foofoofoofoo", "{{ 'foo' | times:4 }}"
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_divided_by
|
||||||
|
assert_template_result "4", "{{ 12 | divided_by:3 }}"
|
||||||
|
assert_template_result "4", "{{ 14 | divided_by:3 }}"
|
||||||
|
assert_template_result "5", "{{ 15 | divided_by:3 }}"
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user