【问题标题】:SonataAdminBundle custom rendering of text fields in listSonataAdminBundle 自定义呈现列表中的文本字段
【发布时间】:2012-02-02 12:09:07
【问题描述】:

我正在使用 symfony2 和 SonataAdminBundle。 我有一个名为 Post 的简单实体,其中的内容字段基本上是 html 文本(来自 ckeditor 的记录)。我需要在 Post 列表中将内容字段显示为原始 html,而不转义它。 像这样破解 base_list_field 模板

{% block field %}{{ value|raw }}{% endblock %}

有效,但显然不是正确的方法。

【问题讨论】:

  • 您应该添加自己的答案并接受它(经过一小段延迟,我认为自我接受需要两天时间),而不是使用解决方案编辑您的答案。

标签: symfony twig sonata-admin


【解决方案1】:

解决办法:

我在 config.yml 中为 sonata_doctrine_orm_admin 定义了一个自定义 html 类型:

sonata_doctrine_orm_admin:
    templates:
      types:
        list:
          html: MyBundle:Default:list_html.html.twig

并创建了我不转义 HTML 的自定义 list_html.html.twig 模板:

{% extends 'SonataAdminBundle:CRUD:base_list_field.html.twig' %}

{% block field%}
    {{value|raw}}
{% endblock %}

现在在 PostAdmin 中,我可以在 configureListFields 方法中定义字段的行为:

$listMapper
    ->add('content', 'html')

【讨论】:

    【解决方案2】:

    我知道这是一个已被接受的旧帖子,但现在您也可以使用 safe 选项告诉 Symfony 不要清理输出。

    $mapper->add('content', null, [
                'safe' => true,
            ]);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-01-14
      • 1970-01-01
      • 1970-01-01
      • 2014-11-03
      • 2020-07-02
      • 2020-06-28
      • 2013-09-09
      • 2017-06-21
      相关资源
      最近更新 更多