mirror of
https://github.com/twigphp/Twig.git
synced 2026-08-30 03:57:21 +00:00
Merge branch '1.x' into 2.x
* 1.x: Bump version Tweak docs Update CHANGELOG Support object init from variable
This commit is contained in:
@@ -321,9 +321,9 @@
|
||||
* improved the performance of the filesystem loader
|
||||
* removed features that were deprecated in 1.x
|
||||
|
||||
# 1.43.2 (2020-XX-XX)
|
||||
# 1.44.0 (2020-XX-XX)
|
||||
|
||||
* n/a
|
||||
* Add support for ES2015 style object initialisation shortcut { a } is the same as { 'a': a }
|
||||
|
||||
# 1.43.1 (2020-08-05)
|
||||
|
||||
|
||||
@@ -560,6 +560,11 @@ exist:
|
||||
{# keys as integer #}
|
||||
{ 2: 'foo', 4: 'bar' }
|
||||
|
||||
{# keys can be omitted if it is the same as the variable name #}
|
||||
{ foo }
|
||||
{# is equivalent to the following #}
|
||||
{ 'foo': foo }
|
||||
|
||||
{# keys as expressions (the expression must be enclosed into parentheses) #}
|
||||
{% set foo = 'foo' %}
|
||||
{ (foo): 'foo', (1 + 1): 'bar', (foo ~ 'b'): 'baz' }
|
||||
|
||||
@@ -366,7 +366,16 @@ class ExpressionParser
|
||||
// * a string -- 'a'
|
||||
// * a name, which is equivalent to a string -- a
|
||||
// * an expression, which must be enclosed in parentheses -- (1 + 2)
|
||||
if (($token = $stream->nextIf(/* Token::STRING_TYPE */ 7)) || ($token = $stream->nextIf(/* Token::NAME_TYPE */ 5)) || $token = $stream->nextIf(/* Token::NUMBER_TYPE */ 6)) {
|
||||
if ($token = $stream->nextIf(/* Token::NAME_TYPE */ 5)) {
|
||||
$key = new ConstantExpression($token->getValue(), $token->getLine());
|
||||
|
||||
// {a} is a shortcut for {a:a}
|
||||
if ($stream->test(Token::PUNCTUATION_TYPE, [',', '}'])) {
|
||||
$value = new NameExpression($key->getAttribute('value'), $key->getTemplateLine());
|
||||
$node->addElement($value, $key);
|
||||
continue;
|
||||
}
|
||||
} elseif (($token = $stream->nextIf(/* Token::STRING_TYPE */ 7)) || $token = $stream->nextIf(/* Token::NUMBER_TYPE */ 6)) {
|
||||
$key = new ConstantExpression($token->getValue(), $token->getLine());
|
||||
} elseif ($stream->test(/* Token::PUNCTUATION_TYPE */ 9, '(')) {
|
||||
$key = $this->parseExpression();
|
||||
|
||||
@@ -84,6 +84,7 @@ class ExpressionParserTest extends TestCase
|
||||
return [
|
||||
['{{ [1, "a": "b"] }}'],
|
||||
['{{ {"a": "b", 2} }}'],
|
||||
['{{ {"a"} }}'],
|
||||
];
|
||||
}
|
||||
|
||||
@@ -160,6 +161,12 @@ class ExpressionParserTest extends TestCase
|
||||
new ConstantExpression('c', 1),
|
||||
], 1),
|
||||
],
|
||||
['{{ {a, b} }}', new ArrayExpression([
|
||||
new ConstantExpression('a', 1),
|
||||
new NameExpression('a', 1),
|
||||
new ConstantExpression('b', 1),
|
||||
new NameExpression('b', 1),
|
||||
], 1)],
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user