Skip to content

Commit

Permalink
Support compound conditions in when clauses (#197)
Browse files Browse the repository at this point in the history
See #187

Fix #124

Co-authored-by: John Rayes <[email protected]>
  • Loading branch information
sanmai and live627 authored Mar 16, 2023
1 parent efb285b commit d8fd54a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 13 deletions.
21 changes: 12 additions & 9 deletions src/Liquid/Tag/TagCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,14 +99,13 @@ public function endTag()
*/
public function unknownTag($tag, $params, array $tokens)
{
$whenSyntaxRegexp = new Regexp('/' . Liquid::get('QUOTED_FRAGMENT') . '/');

switch ($tag) {
case 'when':
$whenSyntax = preg_match_all('/(?<=,|or|^)\s*(' . Liquid::get('QUOTED_FRAGMENT') . ')/', $params, $matches);
// push the current nodelist onto the stack and prepare for a new one
if ($whenSyntaxRegexp->match($params)) {
if ($whenSyntax) {
$this->pushNodelist();
$this->right = $whenSyntaxRegexp->matches[0];
$this->right = $matches[1];
$this->nodelist = array();
} else {
throw new ParseException("Syntax Error in tag 'case' - Valid when condition: when [condition]"); // harry
Expand Down Expand Up @@ -151,12 +150,16 @@ public function render(Context $context)
foreach ($this->nodelists as $data) {
list($right, $nodelist) = $data;

if ($this->equalVariables($this->left, $right, $context)) {
$runElseBlock = false;
foreach ($right as $var) {
if ($this->equalVariables($this->left, $var, $context)) {
$runElseBlock = false;

$context->push();
$output .= $this->renderAll($nodelist, $context);
$context->pop();

$context->push();
$output .= $this->renderAll($nodelist, $context);
$context->pop();
break;
}
}
}

Expand Down
8 changes: 4 additions & 4 deletions tests/Liquid/fixtures/case.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@



... else ...

hit 2 or 3


... else ...
Expand All @@ -26,6 +26,6 @@
2019


May.

Spring
2019

0 comments on commit d8fd54a

Please sign in to comment.