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
$view->getEnvironment->addFilter($yourTwigFilter);
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 }}
or
{{ 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?