Symfony 2:number_format Twig 扩展
我尝试使用 Twig 扩展:
<?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);
}
我在我的包中创建了一个文件夹 Twig/Extension 并将扩展放入其中。
然后我修改了 services.yml 文件来使用它:
services:
project.twig.extension:
class: App\AppBundle\Twig\Extension\NumberExtension
tags:
- { name: twig.extension }
并尝试在某些视图中使用过滤器,如下所示:
{{ 50|number(2, ".", ",") }}
但我收到以下错误:
致命错误:调用未定义的函数 twig_number_filter() C:\wamp\www\myapp\app\cache\dev\twig\de\cc\18a233a6ed21bfc26e40b6654c9c.php 第83行
知道吗?
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 ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试使用
getFilters
方法:Twig_Filter_Function
正在从全局命名空间调用该函数,Twig_Filter_Method
并传递了$this
将调用您的类方法。Try this for the
getFilters
method:Twig_Filter_Function
is calling the function from the global namespace,Twig_Filter_Method
with$this
passed will call your class method.