mirror of
https://github.com/twigphp/Twig.git
synced 2026-09-01 21:17:21 +00:00
add docs & links
This commit is contained in:
committed by
Fabien Potencier
parent
804cb1b85b
commit
dd24d5bfd2
+42
-9
@@ -12,8 +12,8 @@ missing items:
|
|||||||
<table>
|
<table>
|
||||||
{% for row in items|batch(3, 'No item') %}
|
{% for row in items|batch(3, 'No item') %}
|
||||||
<tr>
|
<tr>
|
||||||
{% for column in row %}
|
{% for index, column in row %}
|
||||||
<td>{{ column }}</td>
|
<td>{{ index }} - {{ column }}</td>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
</tr>
|
</tr>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
@@ -25,14 +25,47 @@ The above example will be rendered as:
|
|||||||
|
|
||||||
<table>
|
<table>
|
||||||
<tr>
|
<tr>
|
||||||
<td>a</td>
|
<td>0 - a</td>
|
||||||
<td>b</td>
|
<td>1 - b</td>
|
||||||
<td>c</td>
|
<td>2 - c</td>
|
||||||
</tr>
|
</tr>
|
||||||
<tr>
|
<tr>
|
||||||
<td>d</td>
|
<td>3 - d</td>
|
||||||
<td>No item</td>
|
<td>4 - No item</td>
|
||||||
<td>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>
|
</tr>
|
||||||
</table>
|
</table>
|
||||||
|
|
||||||
@@ -41,4 +74,4 @@ Arguments
|
|||||||
|
|
||||||
* ``size``: The size of the batch; fractional numbers will be rounded up
|
* ``size``: The size of the batch; fractional numbers will be rounded up
|
||||||
* ``fill``: Used to fill in missing items
|
* ``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``)
|
||||||
|
|||||||
@@ -47,7 +47,7 @@ Timezone
|
|||||||
|
|
||||||
By default, the date is displayed by applying the default timezone (the one
|
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
|
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
|
.. code-block:: twig
|
||||||
|
|
||||||
@@ -68,7 +68,7 @@ The default timezone can also be set globally by calling ``setTimezone()``::
|
|||||||
Arguments
|
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
|
* ``timezone``: The date timezone
|
||||||
|
|
||||||
.. _`strtotime`: https://www.php.net/strtotime
|
.. _`strtotime`: https://www.php.net/strtotime
|
||||||
@@ -76,3 +76,4 @@ Arguments
|
|||||||
.. _`DateInterval`: https://www.php.net/DateInterval
|
.. _`DateInterval`: https://www.php.net/DateInterval
|
||||||
.. _`date`: https://www.php.net/date
|
.. _`date`: https://www.php.net/date
|
||||||
.. _`DateInterval::format`: https://www.php.net/DateInterval.format
|
.. _`DateInterval::format`: https://www.php.net/DateInterval.format
|
||||||
|
.. _`timezone`: https://www.php.net/manual/en/timezones.php
|
||||||
|
|||||||
@@ -9,3 +9,9 @@ iterate over the keys of an array:
|
|||||||
{% for key in array|keys %}
|
{% for key in array|keys %}
|
||||||
...
|
...
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
|
|
||||||
|
.. note::
|
||||||
|
|
||||||
|
Internally, Twig uses the PHP `array_keys`_ function.
|
||||||
|
|
||||||
|
.. _`array_keys`: https://www.php.net/array_keys
|
||||||
|
|||||||
@@ -8,3 +8,9 @@ The ``lower`` filter converts a value to lowercase:
|
|||||||
{{ 'WELCOME'|lower }}
|
{{ 'WELCOME'|lower }}
|
||||||
|
|
||||||
{# outputs 'welcome' #}
|
{# outputs 'welcome' #}
|
||||||
|
|
||||||
|
.. note::
|
||||||
|
|
||||||
|
Internally, Twig uses the PHP `mb_strtolower`_ function.
|
||||||
|
|
||||||
|
.. _`mb_strtolower`: https://www.php.net/manual/fr/function.mb-strtolower.php
|
||||||
|
|||||||
@@ -27,3 +27,9 @@ Arguments
|
|||||||
|
|
||||||
* ``arrow``: The arrow function
|
* ``arrow``: The arrow function
|
||||||
* ``initial``: The initial value
|
* ``initial``: The initial value
|
||||||
|
|
||||||
|
.. note::
|
||||||
|
|
||||||
|
Internally, Twig uses the PHP `array_reduce`_ function.
|
||||||
|
|
||||||
|
.. _`array_reduce`: https://www.php.net/array_reduce
|
||||||
|
|||||||
+23
-1
@@ -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
|
variable. If it is omitted, then the sequence will have everything from offset
|
||||||
up until the end of the variable.
|
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::
|
.. note::
|
||||||
|
|
||||||
It also works with objects implementing the `Traversable`_ interface.
|
It also works with objects implementing the `Traversable`_ interface.
|
||||||
@@ -63,7 +85,7 @@ Arguments
|
|||||||
|
|
||||||
* ``start``: The start of the slice
|
* ``start``: The start of the slice
|
||||||
* ``length``: The size 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
|
.. _`Traversable`: https://www.php.net/manual/en/class.traversable.php
|
||||||
.. _`array_slice`: https://www.php.net/array_slice
|
.. _`array_slice`: https://www.php.net/array_slice
|
||||||
|
|||||||
@@ -8,3 +8,9 @@ The ``upper`` filter converts a value to uppercase:
|
|||||||
{{ 'welcome'|upper }}
|
{{ 'welcome'|upper }}
|
||||||
|
|
||||||
{# outputs 'WELCOME' #}
|
{# outputs 'WELCOME' #}
|
||||||
|
|
||||||
|
.. note::
|
||||||
|
|
||||||
|
Internally, Twig uses the PHP `mb_strtoupper`_ function.
|
||||||
|
|
||||||
|
.. _`mb_strtoupper`: https://www.php.net/mb_strtoupper
|
||||||
|
|||||||
Reference in New Issue
Block a user