我创建了一个Twig扩展,以显示不同货币格式的金额,如:印度、美元等。
根据Symfony2指南的建议,我正在按如下方式进行操作。
NameSpace/AccountBundle/Extension/AccountExtension.php
namespace Edu'AccountBundle'Extension;
use Symfony'Component'HttpKernel'KernelInterface;
class AccountTwigExtension extends 'Twig_Extension
{
public function getFilters()
{
return array(
'get_money_indian_format' => new 'Twig_Filter_Method($this, 'get_money_indian_format'),
);
}
function get_money_indian_format($amount, $suffix = 1) {
setlocale(LC_MONETARY, 'en_IN');
if (ctype_digit($amount) ) {
// is whole number
// if not required any numbers after decimal use this format
$amount = money_format('%!.0n', $amount);
} else {
// is not whole number
$amount = money_format('%!i', $amount);
}
if (!$suffix) {
return $amount;
} else {
return $amount;
}
return $amount;
}
public function getName()
{
return 'account_twig_extension';
}
}
在app/config/services.yml
:中注册
account.twig.extension.accounttwigextension:
class: AccountBundle'Extension'AccountTwigExtension
tags:
- { name: twig.extension }
当我在树枝文件中使用这个时:{{ 50000 | get_money_indian_format }}
我收到以下错误:过滤器get_money_indian_format
在EduAccountBundle:Ledger:showLedgers.html.twig
中不存在
如果我按原样复制/粘贴您的代码,我会收到一个错误,因为您的扩展名是
Edu'AccountBundle'ExtensionAccountTwigExtension
而在您的服务定义中,您称之为
AccountBundle'ExtensionAccountTwigExtension
如果我修复了名称空间,一切都会按预期进行。