From: Tyler King Date: Sat, 4 Feb 2012 20:31:37 +0000 (-0330) Subject: Added "split" filter aka explode. X-Git-Url: http://git.silmor.de/gitweb/?a=commitdiff_plain;h=e3c201ad55271eedf93cc6934535ca406d124f96;p=web%2Fkonrad%2Ftwig.git Added "split" filter aka explode. --- diff --git a/doc/filters/index.rst b/doc/filters/index.rst index bd8f495..aca34a9 100644 --- a/doc/filters/index.rst +++ b/doc/filters/index.rst @@ -18,6 +18,7 @@ Filters lower striptags join + split reverse length sort diff --git a/doc/filters/split.rst b/doc/filters/split.rst new file mode 100644 index 0000000..287f823 --- /dev/null +++ b/doc/filters/split.rst @@ -0,0 +1,16 @@ +``split`` +======== + +The ``split`` filter returns a list of items from a string that's separated by the provided delimiter or glue: + +.. code-block:: jinja + + {{ "one,two,three"|split(',') }} + {# returns [one, two, three] #} + +A limit parameter is available which the returned list will contain a maximum of limit elements with the last element containing the rest of string. + +.. code-block:: jinja + + {{ "one,two,three,four,five"|split(',', 3) }} + {# returns [one, two, "three,four,five"] #} diff --git a/lib/Twig/Extension/Core.php b/lib/Twig/Extension/Core.php index 6587f3e..87b3a63 100644 --- a/lib/Twig/Extension/Core.php +++ b/lib/Twig/Extension/Core.php @@ -145,6 +145,7 @@ class Twig_Extension_Core extends Twig_Extension // array helpers 'join' => new Twig_Filter_Function('twig_join_filter'), + 'split' => new Twig_Filter_Function('twig_split_filter'), 'sort' => new Twig_Filter_Function('twig_sort_filter'), 'merge' => new Twig_Filter_Function('twig_array_merge'), @@ -650,6 +651,38 @@ function twig_join_filter($value, $glue = '') return implode($glue, (array) $value); } +/** + * Splits the values into an array. + * + * The second parameter is option for the limit. + * + *
+ *  {{ "one,two,three"|split(',') }}
+ *  {# returns [one, two, three] #}
+ *
+ *  {{ "one,two,three,four,five"|split(',', 3) }}
+ *  {# returns [one, two, "three,four,five"] #}
+ * 
+ * + * @param string $value A string + * @param string $delimiter The separator to explode by + * @param integer $limit The limit + * + * @return string The explode'ed string + */ +function twig_split_filter($value, $delimiter, $limit = null) +{ + if (is_empty($delimiter)) { + throw new Twig_Error_Runtime('The string to split must be provided with a delimiter.'); + } + + if (!is_null($limit)) { + return explode($delimiter, $value, $limit); + } + + return explode($delimiter, $value); +} + // The '_default' filter is used internally to avoid using the ternary operator // which costs a lot for big contexts (before PHP 5.4). So, on average, // a function call is cheaper. diff --git a/test/Twig/Tests/Fixtures/filters/split.test b/test/Twig/Tests/Fixtures/filters/split.test new file mode 100644 index 0000000..2bdcdbf --- /dev/null +++ b/test/Twig/Tests/Fixtures/filters/split.test @@ -0,0 +1,12 @@ +--TEST-- +"split" filter +--TEMPLATE-- +{{ "one,two,three,four,five"|split(',')|join('-') }} +{{ foo|split(',')|join('-') }} +{{ bar|split(',', 3)|join('-') }} +--DATA-- +return array('foo' => "one,two,three,four,five", 'bar' => "one,two,three,four,five") +--EXPECT-- +one-two-three-four-five +one-two-three-four-five +one-two-three,four,five