Add an example on how to iterate over a string

This commit is contained in:
Fabien Potencier
2024-08-30 11:00:01 +02:00
parent 9120e9f3d1
commit a68804d614
2 changed files with 28 additions and 0 deletions
+15
View File
@@ -139,3 +139,18 @@ the :doc:`slice <../filters/slice>` filter:
<li>{{ user.username|e }}</li>
{% endfor %}
</ul>
Iterating over a String
-----------------------
To iterate over the characters of a string, use the
:doc:`split <../filters/split>` filter:
.. code-block:: html+twig
<h1>Characters</h1>
<ul>
{% for char in "諺 / ことわざ"|split('') -%}
<li>{{ char }}</li>
{%- endfor %}
</ul>
@@ -0,0 +1,13 @@
--TEST--
"for" tag can iterate over a string via the "split" filter
--TEMPLATE--
{% set jp = "諺 / ことわざ" %}
{% for letter in jp|split('') -%}
-{{- letter }}
{{- loop.last ? '.' }}
{%- endfor %}
--DATA--
return []
--EXPECT--
-諺- -/- -こ-と-わ-ざ.