Skip to content

Commit e0d54cc

Browse files
committed
fix cycle tag not resetting
1 parent 2a8b75b commit e0d54cc

File tree

2 files changed

+55
-1
lines changed

2 files changed

+55
-1
lines changed

lib/liquid/tags/cycle.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,13 @@ def render_to_output_buffer(context, output)
6868
def variables_from_string(markup)
6969
markup.split(',').collect do |var|
7070
var =~ /\s*(#{QuotedFragment})\s*/o
71-
Regexp.last_match(1) ? parse_expression(Regexp.last_match(1)) : nil
71+
next unless Regexp.last_match(1)
72+
73+
# Expression Parser returns cached objects, and we need to dup them to
74+
# start the cycle over for each new cycle call.
75+
# Liquid-C does not have a cache, so we don't need to dup the object.
76+
var = parse_expression(Regexp.last_match(1))
77+
var.is_a?(VariableLookup) ? var.dup : var
7278
end.compact
7379
end
7480

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# frozen_string_literal: true
2+
3+
require 'test_helper'
4+
5+
class CycleTagTest < Minitest::Test
6+
def test_simple_cycle
7+
template = <<~LIQUID
8+
{%- cycle '1', '2', '3' -%}
9+
{%- cycle '1', '2', '3' -%}
10+
{%- cycle '1', '2', '3' -%}
11+
LIQUID
12+
13+
assert_template_result("123", template)
14+
end
15+
16+
def test_simple_cycle_inside_for_loop
17+
template = <<~LIQUID
18+
{%- for i in (1..3) -%}
19+
{% cycle '1', '2', '3' %}
20+
{%- endfor -%}
21+
LIQUID
22+
23+
assert_template_result("123", template)
24+
end
25+
26+
def test_cycle_with_variables_inside_for_loop
27+
template = <<~LIQUID
28+
{%- assign a = 1 -%}
29+
{%- assign b = 2 -%}
30+
{%- assign c = 3 -%}
31+
{%- for i in (1..3) -%}
32+
{% cycle a, b, c %}
33+
{%- endfor -%}
34+
LIQUID
35+
36+
assert_template_result("123", template)
37+
end
38+
39+
def test_cycle_tag_always_resets_cycle
40+
template = <<~LIQUID
41+
{%- assign a = "1" -%}
42+
{%- cycle a, "2" -%}
43+
{%- cycle a, "2" -%}
44+
LIQUID
45+
46+
assert_template_result("11", template)
47+
end
48+
end

0 commit comments

Comments
 (0)