我从文档中创建了一个简单的Twig过滤器:
public function getFilters() {
return array(
'price' => new \Twig_Filter_Method($this, 'priceFilter'),
);
}
public function priceFilter($number, $decimals = 0, $decPoint = '.', $thousandsSep = ',')
{
$price = number_format($number, $decimals, $decPoint, $thousandsSep);
$price = '$' . $price;
return $price;
}
Run Code Online (Sandbox Code Playgroud)
它已在配置中注册(因为在该文件中我有一个运行良好的功能):
services:
sybio.twig_extension:
class: %sybio.twig_extension.class%
tags:
- { name: twig.extension }
Run Code Online (Sandbox Code Playgroud)
但它说不起作用The filter "price" does not exist.怎么会?
首先要确保 twig 类中有这个函数
public function getName()
{
return 'acme_extension';
}
Run Code Online (Sandbox Code Playgroud)
其次尝试将其更改为完整的类名以进行调试,然后您可以更改它
class: %sybio.twig_extension.class%到class: Acme\DemoBundle\Twig\AcmeExtension
| 归档时间: |
|
| 查看次数: |
3853 次 |
| 最近记录: |