feat: allow liquid filters in for control flow statement
Some checks failed
tests / ci (push) Has been cancelled

This commit is contained in:
Benjamin Nussbaum 2025-08-28 19:20:55 +02:00
parent f777e850b1
commit bcbb1be1da
2 changed files with 111 additions and 0 deletions

View file

@ -230,6 +230,22 @@ class Plugin extends Model
$template
);
// Convert for loops with filters to use temporary variables
// This handles: {% for item in collection | filter: "key", "value" %}
// Converts to: {% assign temp_filtered = collection | filter: "key", "value" %}{% for item in temp_filtered %}
$template = preg_replace_callback(
'/{%\s*for\s+(\w+)\s+in\s+([^|]+)\s*\|\s*([^}]+)%}/',
function ($matches) {
$variableName = trim($matches[1]);
$collection = trim($matches[2]);
$filter = trim($matches[3]);
$tempVarName = '_temp_' . uniqid();
return "{% assign {$tempVarName} = {$collection} | {$filter} %}{% for {$variableName} in {$tempVarName} %}";
},
$template
);
return $template;
}