diff --git a/doc/filters/index.rst b/doc/filters/index.rst index bd8f49513..aca34a92f 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 000000000..287f82392 --- /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 6587f3e67..87b3a63d4 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 000000000..2bdcdbf3c
--- /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