(在列表视图中添加currency
字段非常简单
protected function configureShowFields(ShowMapper $showMapper)
{
$showMapper
->add('price', 'currency', [
'currency' => 'EUR',
'locale' => 'fr',
])
;
}
但是,如果我的货币字符串(欧元,美元,......)来自数据本身(又不是在代码片段中,而是来自数据库表中的字段)?
我可以以某种方式注入currency
字符串吗?
您可以为项目设置自定义模板,然后将对象访问到其中。
protected function configureShowFields(ShowMapper $showMapper)
{
$showMapper
->add('price', 'currency', [
'currency' => 'EUR',
'locale' => 'fr',
'template' => '@AdminTemplates/sonata/show_currency.html.twig',
])
;
}
{# @AdminTemplates/sonata/show_currency.html.twig #}
{% extends '@SonataAdmin/CRUD/base_show_field.html.twig' %}
{%- block field -%}
{% spaceless %}
{%- if value is null -%}
{%- else -%}
{{ value|localizedcurrency(object.currencyField) }}
{%- endif -%}
{% endspaceless %}
{%- endblock field -%}
在这个例子中,我使用了localizedcurrency
中的Twig Intl Extension
如果你使用SonataIntlBundle,你的模板可以扩展show_currency.html.twig,也许你可以覆盖currency
字段选项。
希望这可以帮助