I have created a Twig Extension to show Amounts in different currency format Like: India, USD etc..
I am doing it as follows as suggested by Symfony2 guide.
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';
}
}
Registered it in app/config/services.yml
:
account.twig.extension.accounttwigextension:
class: AccountBundle\Extension\AccountTwigExtension
tags:
- { name: twig.extension }
When I am using this in twig file:
{{ 50000 | get_money_indian_format }}
I am getting following error:
The filter get_money_indian_format
does not exist in EduAccountBundle:Ledger:showLedgers.html.twig
If I copy / paste your code as it is, I get an error because your extension is
Edu\AccountBundle\ExtensionAccountTwigExtension
while in your service definition you call it as
AccountBundle\ExtensionAccountTwigExtension
if I fix the namespace, everything works as expected.