将Symfony2与Sonata一起使用,在列表中,可以覆盖字段模板并将变量分配给模板,即setTemplateVar(),有时候很有用! (不是说'attr'用于此目的的形式,而是列出......)
我想知道将变量传递给configureListFields方法中为listmapper的给定字段定义的模板的最佳方法是什么?
<?php
namespace Acme\AcmeBundle\Admin;
use Sonata\AdminBundle\Admin\Admin;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\AdminBundle\Validator\ErrorElement;
use Sonata\AdminBundle\Form\FormMapper;
class AcmeAdmin extends Admin
{
protected function configureListFields(ListMapper $listMapper)
{
$listMapper
->addIdentifier('acme_field')
->add('date', 'date', array(
'template' => "AcmeBundle:CRUD:list_date.html.twig",
// 'dateFormat' => "Y-m-d",// ---> how to pass this var to twig ?
))
->add('_action', 'actions', array(
'actions' => array(
'edit' => array(),
'delete' => array(),
),
))
;
}
使用twig模板已经实现了翻译和格式化日期的特定问题的解决方案,如下所示:
{% block field%}
{% if value is empty %}
{% else %}
{# retrieving the dateFormat variable from the listmapper #}
{% if dateFormat is defined %}
{{ value|date(dateFormat)|title }}
{% else %}
{{ value|date('m / Y') }}
{% endif %}
{# passing the locale in some way here would be great, it is not available in twig.. #}
{# scratch that, it is not necessary with intl.extension... #}
{% if locale is defined %}
{% set dflt_locale = locale %}
{% else %}
{% set dflt_locale = 'fr_FR.UTF-8' %}
{% endif %}
{{ value|localizeddate('medium', 'none', dflt_locale)|title }}
{% endif %}
{% endblock %}
但是,我的目标是能够从listmapper中检索变量 在提出的示例中,dateFormat将是一个很好的传递...
关于语言环境的问题实际上很好,因为我意识到它不需要传递给localizeddate ...它已经可以通过intl扩展名获得。
以下帖子回答引导我得出结论,因为我无法定义语言环境,但是通过不添加参数它已被解决! Localize dates in twigs using Symfony 2 因此:
{{ value|localizeddate('medium', 'none')|title }}
提前为与dateFormat参数相关的任何响应干杯!
Vinz
答案 0 :(得分:16)
试试Twig
field_description.options.YOURFIELDNAME
这里定义
->add('date', 'date', array(
'template' => "AcmeBundle:CRUD:list_date.html.twig",
'YOURFIELDNAME' => 'Blablo'
))
干杯
答案 1 :(得分:0)
最后,最直接的解决方案是在Admin类中定义var,如下所示:
<?php
namespace Acme\AcmeBundle\Admin;
use Sonata\AdminBundle\Admin\Admin;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\AdminBundle\Validator\ErrorElement;
use Sonata\AdminBundle\Form\FormMapper;
class AcmeAdmin extends Admin
{
// two options to define var and make them available to twig by admin. mechanism
public $dateFormat = "Y-m-d";// ---> this var is available in twig : admin.dateFormat
// or to allow better relation to context
public $parameters = array();// ---> this array is available in twig : admin.parameters.x.y.z
protected function configureListFields(ListMapper $listMapper)
{
// assuming the $dateFormat is not set, by convention it could be defined here with proper intent in mind
// it could then be contained in an array like that
$this->parameters = array('listmapper' => array('dateFormat' => 'Y-m-d',),);
// then specialized in twig : admin.parameters.listmapper.dateFormat
$listMapper
->addIdentifier('acme_field')
->add('date', 'date', array(
'template' => "AcmeBundle:CRUD:list_date.html.twig",// checking admin.parameters.listmapper.dateFormat
))
->add('_action', 'actions', array(
'actions' => array(
'edit' => array(),
'delete' => array(),
),
))
;
}
然后在树枝上:
{% block field%}
{% if value is empty %}
{% else %}
{# retrieving the dateFormat variable from the listmapper by convention...
twig is very forgiving in eval, so checking for defined var is peachy
e.g if admin.parameters is not defined or empty then else is triggered
#}
{% if admin.parameters.listmapper.dateFormat is defined %}
{{ value|date(admin.parameters.listmapper.dateFormat)|title }}
{% else %}
{{ value|localizeddate('medium', 'none')|title }}
{% endif %}
{% endif %}
{% endblock %}
感谢Sonata Admin: Add custom triggers/actions to list/edit action我意识到/记住/连接点......树枝可以访问管理变量,因此通过在类中定义变量,我们可以在树枝中检索它。
现在,为管理变量的容器设置一种约束只是一个常规问题。 (cf参数数组)
对于这个特殊情况,这个解决方案并没有增加任何意义,因为本地化更符合我的目的,但是,为了论证,最好知道恕我直言。
干杯, 希望有所帮助!
Vinz