mirror of
https://github.com/twigphp/Twig.git
synced 2026-09-16 04:16:28 +00:00
added the merge filter
This commit is contained in:
@@ -16,6 +16,7 @@ Backward incompatibilities:
|
||||
|
||||
Changes:
|
||||
|
||||
* added the merge filter
|
||||
* removed 'is_escaper' option for filters (a left over from the previous version) -- you must use 'is_safe' now instead
|
||||
* fixed usage of operators as method names (like is, in, and not)
|
||||
* changed the order of execution for node visitors
|
||||
|
||||
@@ -65,6 +65,7 @@ class Twig_Extension_Core extends Twig_Extension
|
||||
'sort' => new Twig_Filter_Function('twig_sort_filter'),
|
||||
'range' => new Twig_Filter_Function('twig_range_filter'),
|
||||
'cycle' => new Twig_Filter_Function('twig_cycle_filter'),
|
||||
'merge' => new Twig_Filter_Function('twig_array_merge'),
|
||||
|
||||
// iteration and runtime
|
||||
'default' => new Twig_Filter_Function('twig_default_filter'),
|
||||
@@ -187,6 +188,15 @@ function twig_urlencode_filter($url, $raw = false)
|
||||
return urlencode($url);
|
||||
}
|
||||
|
||||
function twig_array_merge($arr1, $arr2)
|
||||
{
|
||||
if (!is_array($arr1) || !is_array($arr2)) {
|
||||
throw new Twig_Error_Runtime('The merge filter only work with arrays or hashes.');
|
||||
}
|
||||
|
||||
return array_merge($arr1, $arr2);
|
||||
}
|
||||
|
||||
function twig_join_filter($value, $glue = '')
|
||||
{
|
||||
return implode($glue, (array) $value);
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
--TEST--
|
||||
"merge" filter
|
||||
--TEMPLATE--
|
||||
{{ items|merge({'bar': 'foo'})|join }}
|
||||
{{ items|merge({'bar': 'foo'})|keys|join }}
|
||||
{{ {'bar': 'foo'}|merge(items)|join }}
|
||||
{{ {'bar': 'foo'}|merge(items)|keys|join }}
|
||||
--DATA--
|
||||
return array('items' => array('foo' => 'bar'))
|
||||
--EXPECT--
|
||||
barfoo
|
||||
foobar
|
||||
foobar
|
||||
barfoo
|
||||
Reference in New Issue
Block a user