Skip to content
This repository

Split filter #621

Closed
wants to merge 11 commits into from
Tyler King

Added "split" filter, felt it was missing from the available filters and needed it for a project.

lib/Twig/Extension/Core.php
((11 lines not shown))
  594
+ *  {# returns [one, two, three] #}
  595
+ *
  596
+ *  {{ "one,two,three,four,five"|split(',', 3) }}
  597
+ *  {# returns [one, two, "three,four,five"] #}
  598
+ * </pre>
  599
+ *
  600
+ * @param string  $value A string
  601
+ * @param string  $glue  The separator
  602
+ * @param integer $limit  The limit
  603
+ *
  604
+ * @return string The explode'ed string
  605
+ */
  606
+function twig_split_filter($value, $glue = '', $limit = null)
  607
+{
  608
+    if (is_null($limit)) {
  609
+        return explode($glue, $value);
1
Christophe Coevoet
stof added a note February 06, 2012

explode does not support empty delimiter (btw, glue is a weird name when splitting)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
lib/Twig/Extension/Core.php
((12 lines not shown))
  595
+ *
  596
+ *  {{ "one,two,three,four,five"|split(',', 3) }}
  597
+ *  {# returns [one, two, "three,four,five"] #}
  598
+ * </pre>
  599
+ *
  600
+ * @param string  $value A string
  601
+ * @param string  $glue  The separator
  602
+ * @param integer $limit  The limit
  603
+ *
  604
+ * @return string The explode'ed string
  605
+ */
  606
+function twig_split_filter($value, $glue = '', $limit = null)
  607
+{
  608
+    if (is_null($limit)) {
  609
+        return explode($glue, $value);
  610
+    } else {
1
Christophe Coevoet
stof added a note February 06, 2012

this else is not needed as the if statement returns

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Christophe Coevoet stof commented on the diff February 06, 2012
test/Twig/Tests/Fixtures/filters/split.test
... ...
@@ -0,0 +1,12 @@
  1
+--TEST--
  2
+"split" filter
  3
+--TEMPLATE--
  4
+{{ "one,two,three,four,five"|split(',')|join('-') }}
  5
+{{ foo|split(',')|join('-') }}
  6
+{{ bar|split(',', 3)|join('-') }}
  7
+--DATA--
  8
+return array('foo' => "one,two,three,four,five", 'bar' => "one,two,three,four,five")
1
Christophe Coevoet
stof added a note February 06, 2012

why using two variables with the same value ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Tyler King

Thanks stof, I have fixed up the things you mentioned. Was bogged down with work at the time and didn't notice ;)

Christophe Coevoet

you broke the indentation by changing it to tabs instead of spaces now. And there is still the issue with empty delimiters

Tyler King

Ah that was my editor forgot to run the command to convert... with regards to the empty delimiter, what do you suggest? If a delimiter is not provided.

Nami-Doc

because I'd like to see that, I suggest using str_split when $delimiter is empty

David ALLIX
webda2l commented May 24, 2012

+1

tacman
tacman commented May 30, 2012

+1 as well. In fact, I thought it was already in there, a quick Google search turned up this:

https://github.com/vito/chyrp/wiki/Twig-Reference

Alas, wrong Twig. Regardless, since Twig handles arrays so well, split would be very useful!

René Moser
resmo commented June 04, 2012

+1

Nicolai Fröhlich
nifr commented June 04, 2012

+1

Henrik Bjørnskov henrikbjorn commented on the diff June 27, 2012
lib/Twig/Extension/Core.php
((10 lines not shown))
  593
+ *  {{ "one,two,three"|split(',') }}
  594
+ *  {# returns [one, two, three] #}
  595
+ *
  596
+ *  {{ "one,two,three,four,five"|split(',', 3) }}
  597
+ *  {# returns [one, two, "three,four,five"] #}
  598
+ * </pre>
  599
+ *
  600
+ * @param string  $value A string
  601
+ * @param string  $delimiter The separator to explode by
  602
+ * @param integer $limit  The limit
  603
+ *
  604
+ * @return string The explode'ed string
  605
+ */
  606
+function twig_split_filter($value, $delimiter, $limit = null)
  607
+{
  608
+    if (is_empty($delimiter)) {
8
Henrik Bjørnskov
henrikbjorn added a note June 27, 2012

Invalid function name it is http://php.net/empty not is_empty

Tobias Schultze
Tobion added a note August 16, 2012

Empty is also wrong ('0'). You probably mean '' == $delimiter

Steven Hoffman
Fustrate added a note August 16, 2012

And then someone uses |split(none) and no exception is thrown. empty($delimiter) && '0' !== $delimiter?

(Edited for a stupid copy/paste mistake)

Constantine Romanov
senz added a note August 16, 2012

@Fustrate, @henrikbjorn already mentioned that is_empty is not a php function.

Tobias Schultze
Tobion added a note August 16, 2012

@Fustrate what is none supposed to mean? There is no such thing in PHP (just as is_empty) ...

Steven Hoffman
Fustrate added a note August 16, 2012

In Twig, none is an alias for null. The dangers of using jinja2 and Twig at the same time...

and you can hit me on the head with a shovel for copy/pasting is_empty($delimiter) instead of typing it out myself. Oops.

Tobias Schultze
Tobion added a note August 16, 2012

null == '' so it works.

Steven Hoffman
Fustrate added a note August 16, 2012

And that's why I stopped using loose comparison... forget I said anything :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Fabien Potencier
Owner
fabpot commented June 27, 2012

Can you squash you commits and fix the remaining bugs? Thanks.

Jonathan Ingram jonathaningram commented on the diff July 01, 2012
lib/Twig/Extension/Core.php
((14 lines not shown))
  597
+ *  {# returns [one, two, "three,four,five"] #}
  598
+ * </pre>
  599
+ *
  600
+ * @param string  $value A string
  601
+ * @param string  $delimiter The separator to explode by
  602
+ * @param integer $limit  The limit
  603
+ *
  604
+ * @return string The explode'ed string
  605
+ */
  606
+function twig_split_filter($value, $delimiter, $limit = null)
  607
+{
  608
+    if (is_empty($delimiter)) {
  609
+        throw new Twig_Error_Runtime('The string to split must be provided with a delimiter.');
  610
+    }
  611
+
  612
+    if (!is_null($limit)) {
2

Maybe null !== $limit? No biggy though...

Constantine Romanov
senz added a note August 16, 2012

This condition is pointless. You can just call return explode($delimiter, $value, $limit); And if limit is null, then it is not used.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Yoander Valdés Rodríguez

Why this pull request is not merged with master-dev?, I've checked https://github.com/aibot/Twig/blob/split-filter/lib/Twig/Extension/Core.php and IMHO is ready for that.

Tyler King
aibot commented July 20, 2012

Sorry guys! Been major busy, will change the code a bit and update it over the next couple days. Going to add str_split as @Nami-Doc suggested.

Yoander Valdés Rodríguez

Well, but in this state the split filter is very usable and it works as expected, and on the other hand the software development is iterative and incremental.

Constantine Romanov
senz commented August 16, 2012

Must have filter. +1

Constantine Romanov senz referenced this pull request August 18, 2012
Closed

Split filter #810

Constantine Romanov senz referenced this pull request August 18, 2012
Merged

Split filter fix #811

Eugene Kubovsky

Hello!

Split is very needed )
When we should expect it to be merged to master?

Thanks!

TWS

This filter is much needed, please merge it ...

Fabien Potencier fabpot referenced this pull request from a commit October 16, 2012
Fabien Potencier merged branch innsand/split-filter_fix (PR #811)
This PR was merged into the master branch.

Commits
-------

d5df200 * bug fixes. * str_split on empty delimeter. * tests and documentation updated.
e3c201a Added "split" filter aka explode.

Discussion
----------

Split filter fix

Modification of request #621:
str_split, bug fixes

---------------------------------------------------------------------------

by senz at 2012-08-21T05:56:51Z

@fabpot this is squashed and fixed (hopefully) #621
a2981f7
Fabien Potencier
Owner

closing in favor of #811 which has been merged.

Fabien Potencier fabpot closed this October 16, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
1  doc/filters/index.rst
Source Rendered
@@ -18,6 +18,7 @@ Filters
18 18
     lower
19 19
     striptags
20 20
     join
  21
+    split
21 22
     reverse
22 23
     length
23 24
     sort
16  doc/filters/split.rst
Source Rendered
... ...
@@ -0,0 +1,16 @@
  1
+``split``
  2
+========
  3
+
  4
+The ``split`` filter returns a list of items from a string that's separated by the provided delimiter or glue:
  5
+
  6
+.. code-block:: jinja
  7
+
  8
+    {{ "one,two,three"|split(',') }}
  9
+    {# returns [one, two, three] #}
  10
+
  11
+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. 
  12
+
  13
+.. code-block:: jinja
  14
+
  15
+    {{ "one,two,three,four,five"|split(',', 3) }}
  16
+    {# returns [one, two, "three,four,five"] #}
33  lib/Twig/Extension/Core.php
@@ -120,6 +120,7 @@ public function getFilters()
120 120
 
121 121
             // array helpers
122 122
             'join'    => new Twig_Filter_Function('twig_join_filter'),
  123
+            'split'   => new Twig_Filter_Function('twig_split_filter'),
123 124
             'sort'    => new Twig_Filter_Function('twig_sort_filter'),
124 125
             'merge'   => new Twig_Filter_Function('twig_array_merge'),
125 126
 
@@ -583,6 +584,38 @@ function twig_join_filter($value, $glue = '')
583 584
     return implode($glue, (array) $value);
584 585
 }
585 586
 
  587
+/**
  588
+ * Splits the values into an array.
  589
+ *
  590
+ * The second parameter is option for the limit.
  591
+ *
  592
+ * <pre>
  593
+ *  {{ "one,two,three"|split(',') }}
  594
+ *  {# returns [one, two, three] #}
  595
+ *
  596
+ *  {{ "one,two,three,four,five"|split(',', 3) }}
  597
+ *  {# returns [one, two, "three,four,five"] #}
  598
+ * </pre>
  599
+ *
  600
+ * @param string  $value A string
  601
+ * @param string  $delimiter The separator to explode by
  602
+ * @param integer $limit  The limit
  603
+ *
  604
+ * @return string The explode'ed string
  605
+ */
  606
+function twig_split_filter($value, $delimiter, $limit = null)
  607
+{
  608
+    if (is_empty($delimiter)) {
  609
+        throw new Twig_Error_Runtime('The string to split must be provided with a delimiter.');
  610
+    }
  611
+
  612
+    if (!is_null($limit)) {
  613
+        return explode($delimiter, $value, $limit);
  614
+    }
  615
+
  616
+    return explode($delimiter, $value);
  617
+}
  618
+
586 619
 // The '_default' filter is used internally to avoid using the ternary operator
587 620
 // which costs a lot for big contexts (before PHP 5.4). So, on average,
588 621
 // a function call is cheaper.
12  test/Twig/Tests/Fixtures/filters/split.test
... ...
@@ -0,0 +1,12 @@
  1
+--TEST--
  2
+"split" filter
  3
+--TEMPLATE--
  4
+{{ "one,two,three,four,five"|split(',')|join('-') }}
  5
+{{ foo|split(',')|join('-') }}
  6
+{{ bar|split(',', 3)|join('-') }}
  7
+--DATA--
  8
+return array('foo' => "one,two,three,four,five", 'bar' => "one,two,three,four,five")
  9
+--EXPECT--
  10
+one-two-three-four-five
  11
+one-two-three-four-five
  12
+one-two-three,four,five
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.