Skip to content
Merged
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
4 changes: 2 additions & 2 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# 3.21.2 (2025-XX-XX)
# 3.22.0 (2025-XX-XX)

* n/a
* Add support for two words test in guard tag

# 3.21.1 (2025-05-03)

Expand Down
8 changes: 7 additions & 1 deletion src/TokenParser/GuardTokenParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,15 @@ public function parse(Token $token): Node
$method = 'get'.$typeToken->getValue();

$nameToken = $stream->expect(Token::NAME_TYPE);
$name = $nameToken->getValue();
if ('test' === $typeToken->getValue() && $stream->test(Token::NAME_TYPE)) {
// try 2-words tests
$name .= ' '.$stream->getCurrent()->getValue();
$stream->next();
}

try {
$exists = null !== $this->parser->getEnvironment()->$method($nameToken->getValue());
$exists = null !== $this->parser->getEnvironment()->$method($name);
} catch (SyntaxError) {
$exists = false;
}
Expand Down
17 changes: 17 additions & 0 deletions tests/Fixtures/tags/guard/basic.test
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,26 @@
{% else %}
NEVER
{% endguard %}

{% guard test foobar %}
NEVER
{{ 'a'|foobar }}
{% else -%}
The foobar test doesn't exist
{% endguard %}

{% guard test divisible by -%}
The divisible by function does exist
{% else %}
NEVER
{% endguard %}
--DATA--
return []
--EXPECT--
The foobar filter doesn't exist

The constant function does exist

The foobar test doesn't exist

The divisible by function does exist
Loading