adding concat filter to append arrays

This commit is contained in:
divecch
2014-08-24 14:45:57 -03:00
committed by Justin Li
parent bebd3570ee
commit db396dd739
4 changed files with 23 additions and 4 deletions

View File

@@ -23,7 +23,7 @@ class Servlet < LiquidServlet
end
def products
{ 'products' => products_list, 'description' => description, 'section' => 'Snowboards', 'cool_products' => true}
{ 'products' => products_list, 'more_products' => more_products_list, 'description' => description, 'section' => 'Snowboards', 'cool_products' => true}
end
private
@@ -34,6 +34,11 @@ class Servlet < LiquidServlet
{'name' => 'Arbor Diamond', 'price' => 59900, 'description' => 'the *arbor diamond* is a made up product because im obsessed with arbor and have no creativity'}]
end
def more_products_list
[{'name' => 'Arbor Catalyst', 'price' => 39900, 'description' => 'the *arbor catalyst* is an advanced drop-through for freestyle and flatground performance and versatility' },
{'name' => 'Arbor Fish', 'price' => 40000, 'description' => 'the *arbor fish* is a compact pin that features an extended wheelbase and time-honored teardrop shape'}]
end
def description
"List of Products ~ This is a list of products with price and description."
end

View File

@@ -16,12 +16,12 @@
</head>
<body>
{% assign all_products = products | concat: more_products %}
<h1>{{ description | split: '~' | first }}</h1>
<h2>{{ description | split: '~' | last }}</h2>
<h2>There are currently {{products | count}} products in the {{section}} catalog</h2>
<h2>There are currently {{all_products | count}} products in the {{section}} catalog</h2>
{% if cool_products %}
Cool products :)
@@ -31,7 +31,7 @@
<ul id="products">
{% for product in products %}
{% for product in all_products %}
<li>
<h2>{{product.name}}</h2>
Only {{product.price | price }}

View File

@@ -177,6 +177,10 @@ module Liquid
input.to_s + string.to_s
end
def concat(input, array)
InputIterator.new(input).concat(array)
end
# prepend a string to another
def prepend(input, string)
string.to_s + input.to_s
@@ -344,6 +348,10 @@ module Liquid
to_a.join(glue)
end
def concat(args)
to_a.concat args
end
def reverse
reverse_each.to_a
end

View File

@@ -358,6 +358,12 @@ class StandardFiltersTest < Minitest::Test
assert_template_result('bcd',"{{ a | append: b}}",assigns)
end
def test_concat
assert_equal([1,2,3,4 ], @filters.concat([1,2], [3,4] ))
assert_equal([1,2,'a' ], @filters.concat([1,2], ['a'] ))
assert_equal([1,2, 10 ], @filters.concat([1,2], [ 10] ))
end
def test_prepend
assigns = {'a' => 'bc', 'b' => 'a' }
assert_template_result('abc',"{{ a | prepend: 'a'}}",assigns)