3

i'm having a problem when i want to use split function in Twig, i have words like this Male\tDoctor ,i want to split that word using \t ascii,

i'm search in search engine and i'm get a discussion about twig split issue in this url i'm read that and i think the problem is fixed, but when i'm search in twig website documentation, i can't found filter function by split you can read this.

and when i'm try using this code

{{ var | split('\t') }) 

the return is The filter "split" does not exist

do you have any idea or some suggestion for solve this problem ?

thanks for your answer...:)

Note :
i'm using fuelphp framework & twig template

viyancs
  • 2,229
  • 4
  • 34
  • 69

3 Answers3

10

just for completeness:
the PR mentioned by F21 is merged now (since 19.10.2012) and twig supports split: http://twig.sensiolabs.org/doc/filters/split.html

{{ "one,two,three"|split(',') }}
{# returns ['one', 'two', 'three'] #}
ivoba
  • 5,283
  • 4
  • 40
  • 50
1

The split function does not yet exist in the Twig master. The pull request you linked to is still open and has not been merged into Twig's repository.

However, you can simply implement the split filter in your own extension. Then once Twig has this filter in its core extensions, simply delete the extension you have created.

Creating your own filter in your own extension is easy: http://twig.sensiolabs.org/doc/advanced.html#filters

F21
  • 28,366
  • 23
  • 91
  • 157
  • that is help me a lot,just how i can create addfilter in fuelphp, did you know where i must place the code for creating custom filter function , i'm search class for fuelphp accessing twig but still not found...thanks for helping..:) – viyancs Jul 11 '12 at 03:58
  • That would really depend on FuelPHP. Where do you usually put your own files in FuelPHP? I use a custom built framework, so I like to put these files in `bridges\twig\extension`, but it is entirely up to you. Once you do that, you just need to point twig to the extension and make it use it. I think this example might help: http://fuelphp.com/forums/topics/view/9617 – F21 Jul 11 '12 at 06:27
  • yes you right..i will post my answer for solve this problem..thanks ..:) – viyancs Jul 11 '12 at 09:19
1

add this function for setup custom filter in twig on fuelphp framework to this file fuel/packages/parser/classes/twig/fuel/extension.php

      /**
        * Returns a list of filters to add to the existing list.
        *
        * @return array An array of filters
        */
        public function getFilters()
        {
            //custom by viyancs adding splite function because not found in twig documentation
             return array(
                 'explode'       => new Twig_Filter_Function('Class::explode_custom', array('pre_escape' => 'html', 'is_safe' => array('html'))),
                 );
        }

and declare explode_custom function in another class in this case you can use same class or another it's depend.

   /*
    * adding custom function for split character
    * used for fuel/app/classes/twig/fuel/extension.php
    * @params
    *  $string : this is twig variable or value example {{ test }}
    *  $split  : this is split character example {{ test\tdata | split('\t') }} \t is split character
    * @return 
    *  array of explode
    * 
    */
    public function explode_custom($string,$split)
    {
        $data = explode($split, $string);
        return $data;
    }

and for using that

{% set varStack = "stack[tab]overflow" | explode('\t') %}
                    {% for value in varStack %}
                    <li>{{ value }}</li>
                    {% endfor %}

result is

stack
overflow
BenMorel
  • 30,280
  • 40
  • 163
  • 285
viyancs
  • 2,229
  • 4
  • 34
  • 69