[SOLVED] Missing addFilter() function in slim/twig-view/src/Twig.php?

Should the Twig.php class from slim/twig-view not contain a


method? I found this post [Slim\Views] how to addFilter() for twig? but the Slim Twig class does not seem to contain an addFunction() method, so I’m a bit confused at this point.

Thanks for any insights.

it has. it even has the addFunction()

I’m sorry, either I’m being stupid or blind (I take this as a sign that it’s really time for me to go to bed).

My slim/twig-view/src/Twig.php does not contain an addFilter() or addFunction() method. The only method it contains matching “add*” is addExtension().

My composer.lock tells me that I have

"version": "1.2.0"

installed which (I just checked) is more than a year old. I’ll update tomorrow morning; my guess is this will fix things.

Lesson learned: don’t blindly copy/paste composer.json entries. :slight_smile:

I just updated and I checked the Twig.php class, and for the life of me, I can’t find an addFunction() method. I’m still getting

Call to undefined method Slim\Views\Twig::addFunction()

Composer.lock now tells me that I have version 2.1.1 installed. OK, I’m going to bed (almost 4AM here), maybe a fresh brain tomorrow will help me figure out what’s going on.

OK, the after some sleep, the now rested brain is still lost over the issue (starting to feel stupid here). To witness:

root@RNG-Linux:/var/www/html/BCM# cd vendor/slim/twig-view/src
root@RNG-Linux:/var/www/html/BCM/vendor/slim/twig-view/src# ls -l
 -rw-r--r-- 1 www-data www-data 1482 Mär 13  2016 TwigExtension.php
 -rw-r--r-- 1 www-data www-data 5971 Mär 13  2016 Twig.php
root@RNG-Linux:/var/www/html/BCM/vendor/slim/twig-view/src# grep addExtension *
Twig.php:    public function addExtension(\Twig_ExtensionInterface $extension)
Twig.php:        $this->environment->addExtension($extension);
root@RNG-Linux:/var/www/html/BCM/vendor/slim/twig-view/src# grep addFunction *
root@RNG-Linux:/var/www/html/BCM/vendor/slim/twig-view/src# grep addFilter *

So I’m not seeing an addFunction() method anywhere.

From composer.lock:

        "name": "slim/twig-view",
        "version": "2.1.1",

This could just be a case of me having stared at this for too long; the solution is probably clear but it’s eluding me. Any hints are highly appreciated.

Hi @TheLobos, I’m far from an expert in this topic so take this with a few grains of salt. I don’t think all of Twig’s methods are directly exposed through slim/twig-view. Some of them you need to reach in and grab from Twig itself rather than through Slim’s bridge.


In that example, $view (slim/twig-view) is getting the Twig Environment (/vendors/twig/twig/lib/Twig/Environment.php) which is where the addFilter() and addFunction() methods live.

But I could be totally misguided here.

Thanks for your suggestion @tflight, it got me 1 big step further. My mistake was (derived from my understanding and from others’ comments) that slim/twig-view exposed this function.

You are correct that it does not that that you have to use


to add a filter. This now works but for some reason the filter I’m using is not being referenced correctly by Twig. Here’s what I’ve got:

class RealtyNumberFormat extends \Twig_SimpleFilter
    public function getName()
       return 'realtyNumberFormat';

    public function getFilters()
        return [
             new \Twig_SimpleFilter('realtyNumberFormat', array($this, 'realtyNumberFormat')),
             new \Twig_SimpleFilter('realtyNumberFormatShort', array($this, 'realtyNumberFormatShort')),
             new \Twig_SimpleFilter('realtyNumberFormatPercentage', array($this, 'realtyNumberFormatPercentage'))

   public function realtyNumberFormat ($string)
       return RealtyManagerUtil::numberFormat($string);

   public function realtyNumberFormatShort ($string)
       return RealtyManagerUtil::numberFormatShort($string);

   public function realtyNumberFormatPercentage ($string)
       return RealtyManagerUtil::numberFormatPercentage($string);

The in the Twig init code I do:

$view->getEnvironment()->addFilter(new RealtyNumberFormat('RealtyManager', RealtyNumberFormat::class));

I find the parameters to addFilter() a bit weird but I’ve tried a number of other stuff, and this one is the only one which Twig doesn’t complain about.

Then in my template I do:

{{ someValue|realtyNumberFormat }}


{{ someValue|realtyNumberFormat() }}

Both of these return the same error:

Call to undefined function RealtyManager\Twig\Filter\RealtyNumberFormat()

Not that it complains about the class name rather than the ‘realtyNumberFormat’ method.

Does anybody know what I’m doing wrong?

After lots of putzing around, I finally figured it out:

  1. Extend your filter from Twig_Extension and NOT from Twig_SimpleFilter

  2. Use addExtension() to add your filter to your Twig instance.

Totally simple; as usual the stupid problems take the longest to solve.