mirror of
https://github.com/Shopify/liquid.git
synced 2026-09-12 23:40:45 -07:00
Implemented reversed flag on for loops {% for a in b reversed %}
This commit is contained in:
@@ -22,6 +22,8 @@ module Liquid
|
|||||||
# {{ item.name }}
|
# {{ item.name }}
|
||||||
# {% end %}
|
# {% end %}
|
||||||
#
|
#
|
||||||
|
# To reverse the for loop simply use {% for item in collection reversed %}
|
||||||
|
#
|
||||||
# == Available variables:
|
# == Available variables:
|
||||||
#
|
#
|
||||||
# forloop.name:: 'item-collection'
|
# forloop.name:: 'item-collection'
|
||||||
@@ -40,13 +42,14 @@ module Liquid
|
|||||||
# forloop.last:: Returns true if the item is the last item.
|
# forloop.last:: Returns true if the item is the last item.
|
||||||
#
|
#
|
||||||
class For < Block
|
class For < Block
|
||||||
Syntax = /(\w+)\s+in\s+(#{VariableSignature}+)/
|
Syntax = /(\w+)\s+in\s+(#{VariableSignature}+)\s*(reversed)?/
|
||||||
|
|
||||||
def initialize(tag_name, markup, tokens)
|
def initialize(tag_name, markup, tokens)
|
||||||
if markup =~ Syntax
|
if markup =~ Syntax
|
||||||
@variable_name = $1
|
@variable_name = $1
|
||||||
@collection_name = $2
|
@collection_name = $2
|
||||||
@name = "#{$1}-#{$2}"
|
@name = "#{$1}-#{$2}"
|
||||||
|
@reversed = $3
|
||||||
@attributes = {}
|
@attributes = {}
|
||||||
markup.scan(TagAttributes) do |key, value|
|
markup.scan(TagAttributes) do |key, value|
|
||||||
@attributes[key] = value
|
@attributes[key] = value
|
||||||
@@ -80,6 +83,8 @@ module Liquid
|
|||||||
|
|
||||||
return '' if segment.empty?
|
return '' if segment.empty?
|
||||||
|
|
||||||
|
segment.reverse! if @reversed
|
||||||
|
|
||||||
result = []
|
result = []
|
||||||
|
|
||||||
length = segment.length
|
length = segment.length
|
||||||
|
|||||||
@@ -376,6 +376,11 @@ HERE
|
|||||||
assert_template_result('', '{% if null == true %}?{% endif %}', {})
|
assert_template_result('', '{% if null == true %}?{% endif %}', {})
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_for_reversed
|
||||||
|
assigns = {'array' => [ 1, 2, 3] }
|
||||||
|
assert_template_result('321','{%for item in array reversed %}{{item}}{%endfor%}',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)
|
||||||
|
|||||||
Reference in New Issue
Block a user