在树枝上显示货币符号 [英] Displaying currency symbol in twig

查看:121
本文介绍了在树枝上显示货币符号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在树枝中显示货币符号?我将符号的数值保存为:

How can I display the currency symbol in twig? I saved the numeric value of the symbol like:

for EURO : €
for DOLLAR: $

渲染这些值时,& 被转换为& 并且货币符号不显示。任何想法将不胜感激。谢谢。

When I render these values, & is converted to & and the currency symbol does not show. Any idea will be greatly appreciated. Thank you.

推荐答案

要做到这一点,您必须添加功能过滤器,它被称为在树枝模板中呈现货币符号的帮助器。

To do it well, you've to add a function or a filter which is called as a helper to render currency symbols within your twig templates.

要使用以下功能,

{{ currency('en_US') }}

您必须如下添加 twig扩展

xxx.twig.your_extension:
    class: XXX\YourBundle\Twig\YourExtension
    tags:
        - { name: twig.extension }

您需要添加一个 currency 函数,

namespace XXX\YourBundle\Twig;

class YourExtension extends \Twig_Extension
{
    public function getFunctions() {
        return array(
            'currency' => new \Twig_Function_Method($this, 'currencyFunction'),
        );
    }

    public function currencyFunction($locale) {
        $formatter = new \NumberFormatter($locale, \NumberFormatter::CURRENCY);
        $symbol = $formatter->getSymbol(\NumberFormatter::CURRENCY_SYMBOL);

        return $symbol;
    }

    public function getName() {
        return 'your_extension';
    }
}

这篇关于在树枝上显示货币符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆