-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
147 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
``shuffle`` | ||
======== | ||
|
||
The ``shuffle`` filter shuffles an array: | ||
|
||
.. code-block:: twig | ||
{% for user in users|shuffle %} | ||
... | ||
{% endfor %} | ||
.. caution:: | ||
|
||
Internally, Twig uses the PHP `shuffle`_ function. | ||
This function assigns new keys to the elements in array. It will remove | ||
any existing keys that may have been assigned, rather than just reordering the keys. | ||
|
||
Example 1: | ||
|
||
.. code-block:: html+twig | ||
|
||
{% set items = [ | ||
'a', | ||
'b', | ||
'c' | ||
] %} | ||
|
||
<ul> | ||
{% for item in items|shuffle %} | ||
<li>{{ item }}</li> | ||
{% endfor %} | ||
</ul> | ||
|
||
The above example will be rendered as: | ||
|
||
.. code-block:: html+twig | ||
|
||
<ul> | ||
<li>a</li> | ||
<li>c</li> | ||
<li>b</li> | ||
</ul> | ||
|
||
Note, results can also be : | ||
"a, b, c" or "b, a, c" or "b, c, a" or "c, a, b" or "c, b, a". | ||
|
||
Example 2: | ||
|
||
.. code-block:: html+twig | ||
|
||
{% set items = [ | ||
'a' => 'd', | ||
'b' => 'e', | ||
'c' => 'f' | ||
] %} | ||
|
||
<ul> | ||
{% for index, item in items|shuffle %} | ||
<li>{{ index }} - {{ item }}</li> | ||
{% endfor %} | ||
</ul> | ||
|
||
The above example will be rendered as: | ||
|
||
.. code-block:: html+twig | ||
|
||
<ul> | ||
<li>0 - d</li> | ||
<li>1 - f</li> | ||
<li>2 - e</li> | ||
</ul> | ||
|
||
Note, results can also be : | ||
"d, e, f" or "e, d, f" or "e, f, d" or "f, d, e" or "f, e, d". | ||
|
||
.. _`shuffle`: https://www.php.net/shuffle |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
--TEST-- | ||
"shuffle" filter | ||
--TEMPLATE-- | ||
{{ array1|shuffle|length }} | ||
{{ array2|shuffle|length }} | ||
{{ array3|shuffle|length }} | ||
{{ traversable|shuffle|length }} | ||
--DATA-- | ||
return ['array1' => [3, 1], 'array2' => ['foo', 'bar'], 'array3' => ['a' => ['e' => 'h'], 'b' => ['f' => 'i'], 'c' => ['g' => 'j']], 'traversable' => new \ArrayObject([0 => 3, 1 => 2, 2 => 1])] | ||
--EXPECT-- | ||
2 | ||
2 | ||
3 | ||
3 |
10 changes: 10 additions & 0 deletions
10
tests/Fixtures/filters/shuffle_with_alphanumerics_characters.test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
--TEST-- | ||
Exception for invalid argument type in "shuffle" filter call | ||
--TEMPLATE-- | ||
{{ array1|shuffle }} | ||
{{ array2|shuffle }} | ||
--DATA-- | ||
return ['array1' => 'Hello World', 'array2' => 1234] | ||
--EXCEPTION-- | ||
The shuffle filter only works with array or "Traversable", got "string" as argument. | ||
The shuffle filter only works with array or "Traversable", got "integer" as argument. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
--TEST-- | ||
Exception for invalid argument type in "shuffle" filter call | ||
--TEMPLATE-- | ||
{{ array|shuffle }} | ||
--DATA-- | ||
return ['array' => false] | ||
--EXCEPTION-- | ||
The shuffle filter only works with array or "Traversable", got "boolean" as argument. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
--TEST-- | ||
Exception for invalid argument type in "shuffle" filter call | ||
--TEMPLATE-- | ||
{{ array|shuffle }} | ||
--DATA-- | ||
return ['array' => ''] | ||
--EXCEPTION-- | ||
The shuffle filter only works with array or "Traversable", got "string" as argument. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
--TEST-- | ||
Exception for invalid argument type in "shuffle" filter call | ||
--TEMPLATE-- | ||
{{ array|shuffle }} | ||
--DATA-- | ||
return ['array' => null] | ||
--EXCEPTION-- | ||
The shuffle filter only works with array or "Traversable", got "NULL" as argument. |