* 1.12.1 (2012-XX-XX)
- * n/a
+ * added support for {{ some_string[:2] }}
* 1.12.0 (2012-01-08)
{{ '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.
} else {
$type = Twig_TemplateInterface::ARRAY_CALL;
- $arg = $this->parseExpression();
-
// slice?
+ $slice = false;
+ if ($stream->test(Twig_Token::PUNCTUATION_TYPE, ':')) {
+ $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 {
{{ [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--
234
234
234
+1