我创建了一个 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