Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
[Twig] How to use a custom extension
  • Hi everyone, This is a question I've been stumbling upon today and haven't been able to find a solution anywhere nor figure one out myself, hope I didn't miss it out. I followed this simple yet very efficient tutorial to truly "install" Twig in FuelPHP : http://yasbas.com/docspro/2011/10/25/enabling-twig-in-fuelphp/ Now I'm trying to figure out the best way to make use of a custom Twig extension. I'll just explain what I've done yet and I'd be pleased to get some feedback about it, as I'm pretty new to Fuel I guess I can learn something useful here. 1) Wrote the extension code itself fuel/app/classes/twig/tom/extension.php ("Tom" being my extension name) :
    <?php
    
    namespace Parser;
    
    use Twig_Extension;
    use Twig_Function_Function;
    use Twig_Function_Method;
    
    /**
     * Nevermind the bullsh*t in there, it's just a test.
     */
    class Twig_Tom_Extension extends Twig_Extension
    {
     /**
      * Gets the name of the extension.
      *
      * @return  string
      */
     public function getName()
     {
      return 'tom';
     }
    
     /**
      * Sets up all of the functions this extension makes available.
      *
      * @return  array
      */
     public function getFunctions()
     {
      return array(
       'myfilter'        => new Twig_Function_Method($this, 'myfilterfunction')
      );
     }
    
     public function myfilterfunction($s)
     {
      return md5($s);
     }
    }
    

    2) Copied /fuel/packages/parser/config/parser.php file to /fuel/app/config/parser.php (overriding it) and declared my extension in it :
    'View_Twig' => array(
      'include' => APPPATH.'vendor'.DS.'Twig'.DS.'Autoloader.php',
      'auto_encode' => true,
      'views_paths' => array(APPPATH.'views'),
      'delimiters' => array(
       'tag_block'     => array('left' => '{%', 'right' => '%}'),
       'tag_comment'   => array('left' => '{#', 'right' => '#}'),
       'tag_variable'  => array('left' => '{{', 'right' => '}}'),
      ),
      'environment' => array(
       'debug'                => false,
       'charset'              => 'utf-8',
       'base_template_class'  => 'Twig_Template',
       'cache'                => APPPATH.'cache'.DS.'twig'.DS,
       'auto_reload'          => true,
       'strict_variables'     => false,
       'autoescape'           => false,
       'optimizations'        => -1,
      ),
      'extensions' => array(
       'Twig_Fuel_Extension',
       'Twig_Tom_Extension'
      ),
     ),
    

    3) Edited /fuel/app/config/config.php to add my class in always_load.classes. Not sure I'm supposed to do so, but if I don't Fuel is unable to find my extension's class (which makes sense in the end). There, when I try it, my extension's correctly loaded but I end up with a wonderful :
    ErrorException [ Error ]: Class 'Twig_Extension' not found

    I'm pretty sure it's a namespace issue or something, I have to admit I'm not very comfortable with this yet so I don't really know what to change to make it work. I might as well be in a totally wrong direction... Thanks in advance to anyone who will help me sort it out, I love Fuel so far and I love Twig as well so I'd really enjoy be able to use Twig at its full power along with Fuel ! =)
  • Yup, I was really going way further than needed for this one, once again I'm pleasefully amazed by how simple and efficient Fuel is. The truth is I was declaring my filter in my extension's getFunctions() method instead of getFilters(), and hadn't noticed... Once this mistake was fixed, it was indeed very simple to set up. The always_load.classes bit was totally useless, just like I felt it would be. Thanks for the link Peter, once I had a good reference it took me 5 mins to sort out !

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion