added the merge filter

This commit is contained in:
Fabien Potencier
2010-12-16 08:10:41 +01:00
parent 211b61c919
commit b792382126
3 changed files with 25 additions and 0 deletions
+1
View File
@@ -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
+10
View File
@@ -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