made it possible to use named operators as variables

This commit is contained in:
Fabien Potencier
2013-10-15 18:27:17 +02:00
parent 6126457550
commit 8d805aacb8
5 changed files with 28 additions and 14 deletions
+1
View File
@@ -1,5 +1,6 @@
* 1.14.1 (2013-XX-XX)
* made it possible to use named operators as variables
* fixed the possibility to have a variable named 'matches'
* added support for PHP 5.5 DateTimeInterface
+3 -2
View File
@@ -162,9 +162,10 @@ class Twig_ExpressionParser
break;
case Twig_Token::OPERATOR_TYPE:
if ('matches' == $token->getValue()) {
if (preg_match(Twig_Lexer::REGEX_NAME, $token->getValue(), $matches) && $matches[0] == $token->getValue()) {
// in this context, string operators are variable names
$this->parser->getStream()->next();
$node = new Twig_Node_Expression_Name('matches', $token->getLine());
$node = new Twig_Node_Expression_Name($token->getValue(), $token->getLine());
break;
}
@@ -0,0 +1,16 @@
--TEST--
Twig allows to use named operators as variable names
--TEMPLATE--
{% for match in matches %}
{{- match }}
{% endfor %}
{{ in }}
{{ is }}
--DATA--
return array('matches' => array(1, 2, 3), 'in' => 'in', 'is' => 'is')
--EXPECT--
1
2
3
in
is
@@ -0,0 +1,8 @@
--TEST--
Twig does not allow to use two-word named operators as variable names
--TEMPLATE--
{{ starts with }}
--DATA--
return array()
--EXCEPTION--
Twig_Error_Syntax: Unexpected token "operator" of value "starts with" in "index.twig" at line 2
@@ -1,12 +0,0 @@
--TEST--
Twig allows to use 'matches' for a variable name
--TEMPLATE--
{% for match in matches %}
{{- match }}
{% endfor %}
--DATA--
return array('matches' => array(1, 2, 3))
--EXPECT--
1
2
3