Skip to content

Commit

Permalink
minor #3941 add docs & links (lacpandore)
Browse files Browse the repository at this point in the history
This PR was squashed before being merged into the 3.x branch.

Discussion
----------

add docs & links

Commits
-------

dd24d5b add docs & links
  • Loading branch information
fabpot committed Dec 14, 2023
2 parents 804cb1b + dd24d5b commit d2f493f
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 12 deletions.
51 changes: 42 additions & 9 deletions doc/filters/batch.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ missing items:
<table>
{% for row in items|batch(3, 'No item') %}
<tr>
{% for column in row %}
<td>{{ column }}</td>
{% for index, column in row %}
<td>{{ index }} - {{ column }}</td>
{% endfor %}
</tr>
{% endfor %}
Expand All @@ -25,14 +25,47 @@ The above example will be rendered as:

<table>
<tr>
<td>a</td>
<td>b</td>
<td>c</td>
<td>0 - a</td>
<td>1 - b</td>
<td>2 - c</td>
</tr>
<tr>
<td>d</td>
<td>No item</td>
<td>No item</td>
<td>3 - d</td>
<td>4 - No item</td>
<td>5 - No item</td>
</tr>
</table>

If you choose to set the third parameter ``preserve_keys`` to ``false``, the keys will be reset in each loop.

.. code-block:: html+twig

{% set items = ['a', 'b', 'c', 'd'] %}

<table>
{% for row in items|batch(3, 'No item', false) %}
<tr>
{% for index, column in row %}
<td>{{ index }} - {{ column }}</td>
{% endfor %}
</tr>
{% endfor %}
</table>

The above example will be rendered as:

.. code-block:: html+twig

<table>
<tr>
<td>0 - a</td>
<td>1 - b</td>
<td>2 - c</td>
</tr>
<tr>
<td>0 - d</td>
<td>1 - No item</td>
<td>2 - No item</td>
</tr>
</table>

Expand All @@ -41,4 +74,4 @@ Arguments

* ``size``: The size of the batch; fractional numbers will be rounded up
* ``fill``: Used to fill in missing items
* ``preserve_keys``: Whether to preserve keys or not
* ``preserve_keys``: Whether to preserve keys or not (defaults to ``true``)
5 changes: 3 additions & 2 deletions doc/filters/date.rst
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ Timezone

By default, the date is displayed by applying the default timezone (the one
specified in php.ini or declared in Twig -- see below), but you can override
it by explicitly specifying a timezone:
it by explicitly specifying a supported `timezone`_:

.. code-block:: twig
Expand All @@ -68,11 +68,12 @@ The default timezone can also be set globally by calling ``setTimezone()``::
Arguments
---------

* ``format``: The date format
* ``format``: The date format (default format is ``F j, Y H:i``, which will render as ``January 11, 2024 15:17``)
* ``timezone``: The date timezone

.. _`strtotime`: https://www.php.net/strtotime
.. _`DateTime`: https://www.php.net/DateTime
.. _`DateInterval`: https://www.php.net/DateInterval
.. _`date`: https://www.php.net/date
.. _`DateInterval::format`: https://www.php.net/DateInterval.format
.. _`timezone`: https://www.php.net/manual/en/timezones.php
6 changes: 6 additions & 0 deletions doc/filters/keys.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,9 @@ iterate over the keys of an array:
{% for key in array|keys %}
...
{% endfor %}
.. note::

Internally, Twig uses the PHP `array_keys`_ function.

.. _`array_keys`: https://www.php.net/array_keys
6 changes: 6 additions & 0 deletions doc/filters/lower.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,9 @@ The ``lower`` filter converts a value to lowercase:
{{ 'WELCOME'|lower }}
{# outputs 'welcome' #}
.. note::

Internally, Twig uses the PHP `mb_strtolower`_ function.

.. _`mb_strtolower`: https://www.php.net/manual/fr/function.mb-strtolower.php
6 changes: 6 additions & 0 deletions doc/filters/reduce.rst
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,9 @@ Arguments

* ``arrow``: The arrow function
* ``initial``: The initial value

.. note::

Internally, Twig uses the PHP `array_reduce`_ function.

.. _`array_reduce`: https://www.php.net/array_reduce
24 changes: 23 additions & 1 deletion doc/filters/slice.rst
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,28 @@ negative then the sequence will stop that many elements from the end of the
variable. If it is omitted, then the sequence will have everything from offset
up until the end of the variable.

The argument ``preserve_keys`` is used to reset the index during the loop.

.. code-block:: twig
{% for key, value in [1, 2, 3, 4, 5]|slice(1, 2, true) %}
{{ key }} - {{ value }}
{% endfor %}
{# output
1 - 2
2 - 3
#}
{% for key, value in [1, 2, 3, 4, 5]|slice(1, 2) %}
{{ key }} - {{ value }}
{% endfor %}
{# output
0 - 2
1 - 3
#}
.. note::

It also works with objects implementing the `Traversable`_ interface.
Expand All @@ -63,7 +85,7 @@ Arguments

* ``start``: The start of the slice
* ``length``: The size of the slice
* ``preserve_keys``: Whether to preserve key or not (when the input is an array)
* ``preserve_keys``: Whether to preserve key or not (when the input is an array), by default the value is ``false``.

.. _`Traversable`: https://www.php.net/manual/en/class.traversable.php
.. _`array_slice`: https://www.php.net/array_slice
Expand Down
6 changes: 6 additions & 0 deletions doc/filters/upper.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,9 @@ The ``upper`` filter converts a value to uppercase:
{{ 'welcome'|upper }}
{# outputs 'WELCOME' #}
.. note::

Internally, Twig uses the PHP `mb_strtoupper`_ function.

.. _`mb_strtoupper`: https://www.php.net/mb_strtoupper

0 comments on commit d2f493f

Please sign in to comment.