【问题标题】:How to easily show newline with Sonata Admin Bundle list view如何使用 Sonata Admin Bundle 列表视图轻松显示换行符
【发布时间】:2014-11-21 02:23:31
【问题描述】:

我应该如何在 SonataAdminBundle 的列表视图表中添加换行符?

我像这样向我的实体添加了一个函数:

getTest() {
    return "test 1 \n test 2";
}

在我的 configureListFields 函数中,我以这种方式添加字段:

$listMapper->add('test', null, array('label' => 'Test'))

我也尝试将类型更改为textstringdoctrine_orm_text,但没有帮助。

使用<br /> 代替\n 并没有帮助,因为输出被转换为html 实体。

【问题讨论】:

    标签: newline symfony-2.1 sonata-admin


    【解决方案1】:

    还有一个更简单的方法:

    protected function configureListFields(ListMapper $listMapper)
    {
        $listMapper
            ->add('message', null, array(
                'template' => 'SensioPlatformBundle:Admin:AuditTrail/message.html.twig',
            ))
        //...
    

    然后在你的模板中:

    <td>
        {{ value|nl2br }}
    </td>
    

    【讨论】:

      【解决方案2】:

      我发现了一种可能性:为文本模板覆盖 base_list_field.html.twig 模板:在定义块 field 时添加 nl2br 过滤器:

      list_text_field.html.twig

      <td class="sonata-ba-list-field sonata-ba-list-field-{{ field_description.type }}" objectId="{{ admin.id(object) }}">
      {% if
              field_description.options.identifier is defined
          and field_description.options.route is defined
          and admin.isGranted(field_description.options.route.name == 'show' ? 'VIEW' : field_description.options.route.name|upper, object)
          and admin.hasRoute(field_description.options.route.name)
      %}
          <a href="{{ admin.generateObjectUrl(field_description.options.route.name, object, field_description.options.route.parameters) }}">
              {%- block field %}{{ value|nl2br }}{% endblock -%}
          </a>
      {% else %}
          {{ block('field') }}
      {% endif %}
      </td>
      

      在你的config.yml

      sonata_doctrine_orm_admin:
          templates:
              types:
                  list:
                      text: YourBundle:Admin:list_text_field.html.twig
      

      configureListFields

      $listMapper->add('test', 'text', array('label' => 'Test'))
      

      【讨论】:

        猜你喜欢
        • 2016-05-31
        • 1970-01-01
        • 2015-05-15
        • 2016-04-20
        • 2018-07-22
        • 2017-04-15
        • 2016-09-03
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多