mirror of
https://github.com/twigphp/Twig.git
synced 2026-09-14 19:36:43 +00:00
Merge branch '1.x' into 2.x
* 1.x: tweaked previous merge Enhances join filter for natural language join (i.e., \"A, B and C\")
This commit is contained in:
@@ -131,7 +131,7 @@
|
||||
|
||||
* 1.36.1 (2018-XX-XX)
|
||||
|
||||
* n/a
|
||||
* added a second argument to the join filter (last separator configuration)
|
||||
|
||||
* 1.36.0 (2018-12-16)
|
||||
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
``join``
|
||||
========
|
||||
|
||||
.. versionadded:: 1.36.1
|
||||
The ``and`` argument was added in Twig 1.36.1.
|
||||
|
||||
The ``join`` filter returns a string which is the concatenation of the items
|
||||
of a sequence:
|
||||
|
||||
@@ -16,8 +19,17 @@ define it with the optional first parameter:
|
||||
|
||||
{{ [1, 2, 3]|join('|') }}
|
||||
{# outputs 1|2|3 #}
|
||||
|
||||
A second parameter can also be provided that will be the separator used between
|
||||
the last two items of the sequence:
|
||||
|
||||
.. code-block:: jinja
|
||||
|
||||
{{ [1, 2, 3]|join(', ', ' and ') }}
|
||||
{# outputs 1, 2 and 3 #}
|
||||
|
||||
Arguments
|
||||
---------
|
||||
|
||||
* ``glue``: The separator
|
||||
* ``and``: The separator for the last pair of input items
|
||||
|
||||
@@ -631,9 +631,12 @@ function twig_last(Twig_Environment $env, $item)
|
||||
/**
|
||||
* Joins the values to a string.
|
||||
*
|
||||
* The separator between elements is an empty string per default, you can define it with the optional parameter.
|
||||
* The separators between elements are empty strings per default, you can define them with the optional parameters.
|
||||
*
|
||||
* <pre>
|
||||
* {{ [1, 2, 3]|join(', ', ' and ') }}
|
||||
* {# returns 1, 2 and 3 #}
|
||||
*
|
||||
* {{ [1, 2, 3]|join('|') }}
|
||||
* {# returns 1|2|3 #}
|
||||
*
|
||||
@@ -641,18 +644,34 @@ function twig_last(Twig_Environment $env, $item)
|
||||
* {# returns 123 #}
|
||||
* </pre>
|
||||
*
|
||||
* @param array $value An array
|
||||
* @param string $glue The separator
|
||||
* @param array $value An array
|
||||
* @param string $glue The separator
|
||||
* @param string|null $and The separator for the last pair
|
||||
*
|
||||
* @return string The concatenated string
|
||||
*/
|
||||
function twig_join_filter($value, $glue = '')
|
||||
function twig_join_filter($value, $glue = '', $and = null)
|
||||
{
|
||||
if ($value instanceof Traversable) {
|
||||
$value = iterator_to_array($value, false);
|
||||
} else {
|
||||
$value = (array) $value;
|
||||
}
|
||||
|
||||
return implode($glue, (array) $value);
|
||||
if (0 === count($value)) {
|
||||
return '';
|
||||
}
|
||||
|
||||
if (null === $and || $and === $glue) {
|
||||
return implode($glue, $value);
|
||||
}
|
||||
|
||||
$v = array_values($value);
|
||||
if (1 === count($v)) {
|
||||
return $v[0];
|
||||
}
|
||||
|
||||
return implode($glue, array_slice($value, 0, -1)).$and.$v[count($v) - 1];
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -4,9 +4,35 @@
|
||||
{{ ["foo", "bar"]|join(', ') }}
|
||||
{{ foo|join(', ') }}
|
||||
{{ bar|join(', ') }}
|
||||
|
||||
{{ ["foo", "bar"]|join(', ', ' and ') }}
|
||||
{{ foo|join(', ', ' and ') }}
|
||||
{{ bar|join(', ', ' and ') }}
|
||||
{{ ["one", "two", "three"]|join(', ', ' and ') }}
|
||||
{{ ["a", "b", "c"]|join('','-') }}
|
||||
{{ ["a", "b", "c"]|join('-','-') }}
|
||||
{{ ["a", "b", "c"]|join('-','') }}
|
||||
{{ ["hello"]|join('|','-') }}
|
||||
|
||||
{{ {"a": "w", "b": "x", "c": "y", "d": "z"}|join }}
|
||||
{{ {"a": "w", "b": "x", "c": "y", "d": "z"}|join(',') }}
|
||||
{{ {"a": "w", "b": "x", "c": "y", "d": "z"}|join(',','-') }}
|
||||
--DATA--
|
||||
return array('foo' => new TwigTestFoo(), 'bar' => new ArrayObject(array(3, 4)))
|
||||
--EXPECT--
|
||||
foo, bar
|
||||
1, 2
|
||||
3, 4
|
||||
|
||||
foo and bar
|
||||
1 and 2
|
||||
3 and 4
|
||||
one, two and three
|
||||
ab-c
|
||||
a-b-c
|
||||
a-bc
|
||||
hello
|
||||
|
||||
wxyz
|
||||
w,x,y,z
|
||||
w,x,y-z
|
||||
|
||||
Reference in New Issue
Block a user