Browse code

Update slice.rst

I was (somehow) misled by the example, thinking `'1234'[2:]` outputs `34` because it `2:` mean copy the last 2 characters...

This happens when you are in a hurry and didn't look into the text.

Let makes it less ambiguous.

Peter WONG authored on 29/05/2013 03:18:36
Showing 1 changed files
... ...
@@ -8,11 +8,11 @@ The ``slice`` filter extracts a slice of a sequence, a mapping, or a string:
8 8
 
9 9
 .. code-block:: jinja
10 10
 
11
-    {% for i in [1, 2, 3, 4]|slice(1, 2) %}
11
+    {% for i in [1, 2, 3, 4, 5]|slice(1, 2) %}
12 12
         {# will iterate over 2 and 3 #}
13 13
     {% endfor %}
14 14
 
15
-    {{ '1234'|slice(1, 2) }}
15
+    {{ '12345'|slice(1, 2) }}
16 16
 
17 17
     {# outputs 23 #}
18 18
 
... ...
@@ -20,7 +20,7 @@ You can use any valid expression for both the start and the length:
20 20
 
21 21
 .. code-block:: jinja
22 22
 
23
-    {% for i in [1, 2, 3, 4]|slice(start, length) %}
23
+    {% for i in [1, 2, 3, 4, 5]|slice(start, length) %}
24 24
         {# ... #}
25 25
     {% endfor %}
26 26
 
... ...
@@ -28,17 +28,17 @@ As syntactic sugar, you can also use the ``[]`` notation:
28 28
 
29 29
 .. code-block:: jinja
30 30
 
31
-    {% for i in [1, 2, 3, 4][start:length] %}
31
+    {% for i in [1, 2, 3, 4, 5][start:length] %}
32 32
         {# ... #}
33 33
     {% endfor %}
34 34
 
35
-    {{ '1234'[1:2] }}
35
+    {{ '12345'[1:2] }}
36 36
 
37 37
     {# you can omit the first argument -- which is the same as 0 #}
38
-    {{ '1234'[:2] }} {# will display "12" #}
38
+    {{ '12345'[:2] }} {# will display "12" #}
39 39
 
40 40
     {# you can omit the last argument -- which will select everything till the end #}
41
-    {{ '1234'[2:] }} {# will display "34 #}
41
+    {{ '12345'[2:] }} {# will display "345" #}
42 42
 
43 43
 The ``slice`` filter works as the `array_slice`_ PHP function for arrays and
44 44
 `substr`_ for strings.