Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 9 additions & 32 deletions src/Liquid/Tag/TagCycle.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,15 @@ class TagCycle extends AbstractTag
*/
public function __construct($markup, array &$tokens, FileSystem $fileSystem = null)
{
$simpleSyntax = new Regexp("/" . Liquid::get('QUOTED_FRAGMENT') . "/");
$namedSyntax = new Regexp("/(" . Liquid::get('QUOTED_FRAGMENT') . ")\s*\:\s*(.*)/");

if ($namedSyntax->match($markup)) {
$this->variables = $this->variablesFromString($namedSyntax->matches[2]);
$this->name = $namedSyntax->matches[1];
} elseif ($simpleSyntax->match($markup)) {
$this->variables = $this->variablesFromString($markup);
$simpleSyntax = preg_match_all('/(["\'])?(?(1)(?:(?!\1).)*+\1|[^\s,:|\'"]++)/', $markup, $matches);
$namedSyntax = preg_match('/^(["\'])?(?(1)(?:(?!\1).)*+\1|[^\s,:|\'"]++)\s*\:/', $markup, $namedMatches);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm somewhat concerned about dropping Liquid::get('QUOTED_FRAGMENT') here 🤔


if ($namedSyntax) {
$this->name = $matches[0][0];
array_shift($matches[0]);
$this->variables = $matches[0];
} elseif ($simpleSyntax) {
$this->variables = $matches[0];
$this->name = "'" . implode($this->variables) . "'";
} else {
throw new ParseException("Syntax Error in 'cycle' - Valid syntax: cycle [name :] var [, var2, var3 ...]");
Expand Down Expand Up @@ -103,28 +104,4 @@ public function render(Context $context)

return $result;
}

/**
* Extract variables from a string of markup
*
* @param string $markup
*
* @return array;
*/
private function variablesFromString($markup)
{
$regexp = new Regexp('/\s*(' . Liquid::get('QUOTED_FRAGMENT') . ')\s*/');
$parts = explode(',', $markup);
$result = array();

foreach ($parts as $part) {
$regexp->match($part);

if (!empty($regexp->matches[1])) {
$result[] = $regexp->matches[1];
}
}

return $result;
}
}
6 changes: 5 additions & 1 deletion tests/Liquid/Tag/TagCycleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,15 @@ public function testCycle()
public function testMultipleCycles()
{
$this->assertTemplateResult('1 2 1 1 2 3 1', '{%cycle 1,2%} {%cycle 1,2%} {%cycle 1,2%} {%cycle 1,2,3%} {%cycle 1,2,3%} {%cycle 1,2,3%} {%cycle 1,2,3%}');
$this->assertTemplateResult('onetwo:,three', '{%cycle "one","two:,three"%}{%cycle "one","two:,three"%}');
}

public function testMultipleNamedCycles()
{
$this->assertTemplateResult('one one two two one one', '{%cycle 1: "one", "two" %} {%cycle 2: "one", "two" %} {%cycle 1: "one", "two" %} {%cycle 2: "one", "two" %} {%cycle 1: "one", "two" %} {%cycle 2: "one", "two" %}');
$this->assertTemplateResult('one one two two one one', '{%cycle 1: "one", "two" %} {%cycle 2: "one", "two" %} {%cycle 1 : "one", "two" %} {%cycle 2 : "one", "two" %} {%cycle 1: "one", "two" %} {%cycle 2: "one", "two" %}');
$this->assertTemplateResult('one one two two one one', '{%cycle "first": "one", "two" %} {%cycle "second": "one", "two" %} {%cycle "first" : "one", "two" %} {%cycle "second" : "one", "two" %} {%cycle "first": "one", "two" %} {%cycle "second": "one", "two" %}');
$this->assertTemplateResult('one one two two one one', '{%cycle 1: \'one\', \'two\' %} {%cycle 2: \'one\', \'two\' %} {%cycle 1 : \'one\', \'two\' %} {%cycle 2 : \'one\', \'two\' %} {%cycle 1: \'one\', \'two\' %} {%cycle 2: \'one\', \'two\' %}');
$this->assertTemplateResult('one one two two one one', '{%cycle \'first\': \'one\', \'two\' %} {%cycle \'second\': \'one\', \'two\' %} {%cycle \'first\' : \'one\', \'two\' %} {%cycle \'second\' : \'one\', \'two\' %} {%cycle \'first\': \'one\', \'two\' %} {%cycle \'second\': \'one\', \'two\' %}');
}

public function testMultipleNamedCyclesWithNamesFromContext()
Expand Down
Loading