2

我有一个显示页面,我想添加一个自定义值。

我已经尝试做我在其他操作中所做的事情,即使用 data 键将数组添加到第三个参数,如下所示:

protected function configureShowFields(ShowMapper $showMapper)
{
    $showMapper
        ->add('name')                      
        ->add('dateEnd')
        ->add('example', null,
            array('data' => 'example value')
        )
    ;
}

在 configureListFields 操作中,这是可行的。我已经用 data 属性注入了自定义值。

但我仍然无法访问show.html.twig文件中的关键示例。它给了我这个错误

变量“示例”不存在。

我应该怎么做才能访问树枝文件中的这个自定义变量?

4

2 回答 2

1

尝试

{{ elements.elements.example.options.data }} 

在你的树枝模板中

于 2017-11-10T13:32:14.720 回答
0

我使用了这个解决方案。在configureShowFields()Admin 类的方法中:

$showMapper
        ->with('Tab Name')
        ->add(
            'any_name',
            null,
            [
                'template' => 'Admin/Custom/any_name_show_template.html.twig',
                'customData' => $this->someRepository->getSomeEntityBy($field),
                'anotherCustomData' => $this->someService->getSomeDataBy($value),
            ]
        )
    ;

在自定义模板中,您可以通过 访问自定义数据field_description.options.<customFieldName>,因此对于提供的示例数据访问器将是{{ field_description.options.customData }}{{ field_description.options.anotherCustomData }}

对于 Twig 模板中较短的字段名称,您可以这样做:

{% set customData = field_description.options.customData %}

并访问自定义数据,例如{{ customData }}

希望这会有所帮助并节省时间。

于 2021-10-27T14:28:33.627 回答