mirror of
https://github.com/twigphp/Twig.git
synced 2026-09-16 04:16:28 +00:00
Adding a section about the slice filter
This code example shows how to loop over a subset of values in order to implement one common use-case of "break" in PHP. I decided to use the "full" notation of slice rather than the sugared version (that example would just be "for user in users[:10]")
This commit is contained in:
@@ -155,3 +155,17 @@ You can also access both keys and values:
|
|||||||
<li>{{ key }}: {{ user.username|e }}</li>
|
<li>{{ key }}: {{ user.username|e }}</li>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
</ul>
|
</ul>
|
||||||
|
|
||||||
|
Iterating over Subset
|
||||||
|
---------------------
|
||||||
|
|
||||||
|
You might want to iterate over a subset of values. This can be achieved using the ``slice`` filter:
|
||||||
|
|
||||||
|
.. code-block:: jinja
|
||||||
|
|
||||||
|
<h1>Top Ten Members</h1>
|
||||||
|
<ul>
|
||||||
|
{% for user in users|slice(0,10) %}
|
||||||
|
<li>{{ user.username|e }}</li>
|
||||||
|
{% endfor %}
|
||||||
|
</ul>
|
||||||
|
|||||||
Reference in New Issue
Block a user