added support for {{ some_string[:2] }} (closes #952)

This commit is contained in:
Fabien Potencier
2013-01-08 15:19:16 +01:00
parent ad31e1765a
commit d10ebfa3d4
4 changed files with 21 additions and 4 deletions
+1 -1
View File
@@ -1,6 +1,6 @@
* 1.12.1 (2012-XX-XX)
* n/a
* added support for {{ some_string[:2] }}
* 1.12.0 (2012-01-08)
+6
View File
@@ -34,6 +34,12 @@ As syntactic sugar, you can also use the ``[]`` notation:
{{ '1234'[1:2] }}
{# you can omit the first argument -- which is the same as 0 #}
{{ '1234'[:2] }} {# will display "12" #}
{# you can omit the last argument -- which will select everything till the end #}
{{ '1234'[2:] }} {# will display "34 #}
The ``slice`` filter works as the `array_slice`_ PHP function for arrays and
`substr`_ for strings.
+12 -3
View File
@@ -379,12 +379,21 @@ class Twig_ExpressionParser
} else {
$type = Twig_TemplateInterface::ARRAY_CALL;
$arg = $this->parseExpression();
// slice?
$slice = false;
if ($stream->test(Twig_Token::PUNCTUATION_TYPE, ':')) {
$stream->next();
$slice = true;
$arg = new Twig_Node_Expression_Constant(0, $token->getLine());
} else {
$arg = $this->parseExpression();
}
if ($stream->test(Twig_Token::PUNCTUATION_TYPE, ':')) {
$slice = true;
$stream->next();
}
if ($slice) {
if ($stream->test(Twig_Token::PUNCTUATION_TYPE, ']')) {
$length = new Twig_Node_Expression_Constant(null, $token->getLine());
} else {
@@ -18,6 +18,7 @@
{{ [1, 2, 3, 4][1:]|join('') }}
{{ '1234'|slice(1) }}
{{ '1234'[1:] }}
{{ '1234'[:1] }}
--DATA--
return array('start' => 1, 'length' => 2, 'arr' => new ArrayObject(array(1, 2, 3, 4)))
--EXPECT--
@@ -38,3 +39,4 @@ bc
234
234
234
1