Reputation: 657
I tried to use that Twig Extension :
<?php
class Twig_Extensions_Extension_Number extends Twig_Extension
{
/**
* Returns a list of filters.
*
* @return array
*/
public function getFilters()
{
return array('number' => new Twig_Filter_Function('twig_number_filter'));
}
/**
* Name of this extension
*
* @return string
*/
public function getName()
{
return 'Number';
}
}
function twig_number_filter($number, $decimals = 0, $dec_point = '.', $thousands_sep = ',')
{
return number_format($number, $decimals, $dec_point, $thousands_sep);
}
I've created a folder Twig/Extension in my bundle and put the Extension inside.
Then I modified the services.yml file to use it :
services:
project.twig.extension:
class: App\AppBundle\Twig\Extension\NumberExtension
tags:
- { name: twig.extension }
And tried to use the filter in some view like this :
{{ 50|number(2, ".", ",") }}
But I got the following error :
Fatal error: Call to undefined function twig_number_filter() in C:\wamp\www\myapp\app\cache\dev\twig\de\cc\18a233a6ed21bfc26e40b6654c9c.php on line 83
Any idea ?
Upvotes: 1
Views: 3182
Reputation: 17678
Try this for the getFilters
method:
public function getFilters()
{
return array('number' => new \Twig_Filter_Method($this, 'twig_number_filter'));
}
Twig_Filter_Function
is calling the function from the global namespace, Twig_Filter_Method
with $this
passed will call your class method.
Upvotes: 1