3

将 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 %}
            &nbsp;
        {% 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 将是一个很好的传递...

关于语言环境的问题实际上很好,因为我意识到它不需要传递给本地化日期......它已经可以通过 intl 扩展获得。

以下帖子答案使我得出这个结论,因为我无法定义语言环境,但通过不添加参数,它已解决! 使用 Symfony 2 本地化树枝中的日期, 因此:

    {{ value|localizeddate('medium', 'none')|title }}

提前为与 dateFormat 参数相关的任何响应干杯!

文兹

4

2 回答 2

16

在树枝上试试

field_description.options.YOURFIELDNAME

在这里定义

->add('date', 'date', array(
                    'template'   => "AcmeBundle:CRUD:list_date.html.twig",
                    'YOURFIELDNAME' => 'Blablo'
                ))

干杯

于 2013-09-26T14:00:08.480 回答
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 %}
                &nbsp;
            {% 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:将自定义触发器/操作添加到列表/编辑操作我意识到/记住/连接了这些点......树枝可以访问 admin 变量,因此通过在类中定义一个变量,我们可以在 twig 中检索它。

现在,对管理变量的容器设置某种约束只是一个约定问题。(cf 参数数组)

对于这种特殊情况,此解决方案不会添加任何点,因为本地化日期很好地满足了我的目的,但是,为了论证,了解恕我直言是件好事。

干杯,希望有帮助!

文兹

于 2013-03-28T11:10:59.483 回答