mirror of
https://github.com/twigphp/Twig.git
synced 2026-09-15 11:56:50 +00:00
added support for {{ some_string[:2] }} (closes #952)
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
* 1.12.1 (2012-XX-XX)
|
||||
|
||||
* n/a
|
||||
* added support for {{ some_string[:2] }}
|
||||
|
||||
* 1.12.0 (2012-01-08)
|
||||
|
||||
|
||||
@@ -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.
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user