Browse files

changed hash key expression support (expression must now be enclosed …

…with parentheses and hash keys can now be names)
  • Loading branch information...
1 parent 0d48b6d commit f3de043d0eabd7d5369414fa1e3d3d99b0418d79 @fabpot committed Dec 26, 2011
Showing with 43 additions and 5 deletions.
  1. +1 −0 CHANGELOG
  2. +18 −3 doc/templates.rst
  3. +17 −1 lib/Twig/ExpressionParser.php
  4. +7 −1 test/Twig/Tests/Fixtures/expressions/array.test
View
1 CHANGELOG
@@ -1,5 +1,6 @@
* 1.5.0
+ * removed the need to quote hash keys
* allowed hash keys to be any expression
* added a do tag
* added a flush tag
View
21 doc/templates.rst
@@ -458,6 +458,9 @@ even if you're not working with PHP you should feel comfortable with it.
Literals
~~~~~~~~
+.. versionadded:: 1.5
+ Support for hash keys as names and expressions was added in Twig 1.5.
+
The simplest form of expressions are literals. Literals are representations
for PHP types such as strings, numbers, and arrays. The following literals
exist:
@@ -475,9 +478,21 @@ exist:
separated by a comma (``,``) and wrapped with squared brackets (``[]``).
* ``{"foo": "bar"}``: Hashes are defined by a list of keys and values
- separated by a comma (``,``) and wrapped with curly braces (``{}``). A value
- can be any valid expression (as of Twig 1.5, keys can also be any valid
- expression).
+ separated by a comma (``,``) and wrapped with curly braces (``{}``):
+
+ .. node-block:: jinja
+
+ # keys as string
+ { 'foo': 'foo', 'bar': 'bar' }
+
+ # keys as names (equivalent to the previous hash) -- as of Twig 1.5
+ { foo: 'foo', bar: 'bar' }
+
+ # keys as integer
+ { 2: 'foo', 4: 'bar' }
+
+ # keys as expressions (the expression must be enclosed into parentheses) -- as of Twig 1.5
+ { (1 + 1): 'foo', (a ~ 'b'): 'bar' }
* ``true`` / ``false``: ``true`` represents the true value, ``false``
represents the false value.
View
18 lib/Twig/ExpressionParser.php
@@ -236,7 +236,23 @@ public function parseHashExpression()
}
$first = false;
- $key = $this->parseExpression();
+ // a hash key can be:
+ //
+ // * a number -- 12
+ // * a string -- 'a'
+ // * a name, which is equivalent to a string -- a
+ // * an expression, which must be enclosed in parentheses -- (1 + 2)
+ if ($stream->test(Twig_Token::STRING_TYPE) || $stream->test(Twig_Token::NAME_TYPE) || $stream->test(Twig_Token::NUMBER_TYPE)) {
+ $token = $stream->next();
+ $key = new Twig_Node_Expression_Constant($token->getValue(), $token->getLine());
+ } elseif ($stream->test(Twig_Token::PUNCTUATION_TYPE, '(')) {
+ $key = $this->parseExpression();
+ } else {
+ $current = $stream->getCurrent();
+
+ throw new Twig_Error_Syntax(sprintf('A hash key must be a quoted string, a number, a name, or an expression enclosed in parentheses (unexpected token "%s" of value "%s"', Twig_Token::typeToEnglish($current->getType(), $current->getLine()), $current->getValue()), $current->getLine());
+ }
+
$stream->expect(Twig_Token::PUNCTUATION_TYPE, ':', 'A hash key must be followed by a colon (:)');
$value = $this->parseExpression();
View
8 test/Twig/Tests/Fixtures/expressions/array.test
@@ -9,6 +9,9 @@ Twig supports array notation
{{ {0: 1, 'foo': 'bar'}|join(',') }}
{{ {0: 1, 'foo': 'bar'}|keys|join(',') }}
+{{ {0: 1, foo: 'bar'}|join(',') }}
+{{ {0: 1, foo: 'bar'}|keys|join(',') }}
+
{# nested arrays #}
{% set a = [1, 2, [1, 2], {'foo': {'foo': 'bar'}}] %}
{{ a[2]|join(',') }}
@@ -31,7 +34,7 @@ Twig supports array notation
{# keys can be any expression #}
{% set a = 1 %}
{% set b = "foo" %}
-{% set ary = { a: 'a', b: 'b', 'c': 'c', a~b: 'd' } %}
+{% set ary = { (a): 'a', (b): 'b', 'c': 'c', (a ~ b): 'd' } %}
{{ ary|keys|join(',') }}
{{ ary|join(',') }}
--DATA--
@@ -42,6 +45,9 @@ foo,bar
1,bar
0,foo
+1,bar
+0,foo
+
1,2
bar

0 comments on commit f3de043

Please sign in to comment.