From b79238212657d3d2fb0829f5fc1472534edc3a0b Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Thu, 16 Dec 2010 08:10:41 +0100 Subject: [PATCH] added the merge filter --- CHANGELOG | 1 + lib/Twig/Extension/Core.php | 10 ++++++++++ test/Twig/Tests/Fixtures/filters/merge.test | 14 ++++++++++++++ 3 files changed, 25 insertions(+), 0 deletions(-) create mode 100644 test/Twig/Tests/Fixtures/filters/merge.test diff --git a/CHANGELOG b/CHANGELOG index e42511f..e97f2ea 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -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 diff --git a/lib/Twig/Extension/Core.php b/lib/Twig/Extension/Core.php index cb3a110..506e03d 100644 --- a/lib/Twig/Extension/Core.php +++ b/lib/Twig/Extension/Core.php @@ -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); diff --git a/test/Twig/Tests/Fixtures/filters/merge.test b/test/Twig/Tests/Fixtures/filters/merge.test new file mode 100644 index 0000000..357b352 --- /dev/null +++ b/test/Twig/Tests/Fixtures/filters/merge.test @@ -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 -- 1.7.2.5