Skip to content
  • About
    • What is Symfony?
    • Community
    • News
    • Contributing
    • Support
  • Documentation
    • Symfony Docs
    • Symfony Book
    • Screencasts
    • Symfony Bundles
    • Symfony Cloud
    • Training
  • Services
    • Platform.sh for Symfony Best platform to deploy Symfony apps
    • SymfonyInsight Automatic quality checks for your apps
    • Symfony Certification Prove your knowledge and boost your career
    • SensioLabs Professional services to help you with Symfony
    • Blackfire Profile and monitor performance of your apps
  • Other
  • Blog
  • Download
sponsored by
SymfonyOnline January 2025
January 16-17
100% Online
+25 talks and workshops
  1. Home
  2. Documentation
  3. Bundles
  4. SonataAdminBundle
  5. Row templates

Row templates

Edit this page

Since Sonata-2.2 it is possible to define a template per row for the list action. The default template is a standard table but there are circumstances where this type of layout might not be suitable. By defining a custom template for the row, you can tweak the layout into something like this:

Inline Row from the SonataNewsBundle

The recipe

Configure your Admin service

The configuration takes place in the DIC by calling the setTemplates method. Two template keys need to be set:

  • inner_list_row: The template for the row, which you will customize. Often you will want this to extend @SonataAdmin/CRUD/base_list_flat_inner_row.html.twig
  • base_list_field: The base template for the cell, the default of @SonataAdmin/CRUD/base_list_flat_field.html.twig is suitable for most cases but it can be customized if required.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<!-- config/services.xml -->

<service id="sonata.admin.comment" class="%sonata.admin.comment.class%">
    <call method="setTemplates">
        <argument type="collection">
            <argument key="inner_list_row">
                @App/Admin/inner_row_comment.html.twig
            </argument>
            <argument key="base_list_field">
                @SonataAdmin/CRUD/base_list_flat_field.html.twig
            </argument>
        </argument>
    </call>
    <tag
        name="sonata.admin"
        model_class="%sonata.admin.comment.entity%"
        controller="%sonata.admin.comment.controller%"
        manager_type="orm"
        group="sonata_blog"
        translation_domain="%sonata.admin.comment.translation_domain%"
        label="comments"
        label_translator_strategy="sonata.admin.label.strategy.underscore"
        />
</service>

Create your customized template

Once the templates are defined, create the template to render the row:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
{# @App/Admin/inner_row_comment.html.twig #}

{# Extend the default template, which provides batch and action cells #}
{#     as well as the valid colspan computation #}
{% extends '@SonataAdmin/CRUD/base_list_flat_inner_row.html.twig' %}

{% block row %}

    {# you can use fields defined in the the Admin class #}

    {{ object|render_list_element(admin.list['name']) }} -
    {{ object|render_list_element(admin.list['url']) }} -
    {{ object|render_list_element(admin.list['email']) }} <br/>

    <small>
        {# or you can use the object variable to render a property #}
        {{ object.message }}
    </small>

{% endblock %}

While this feature is nice to generate a rich list, you can break the layout and admin features such as batch and object actions. It is best to familiarise yourself with the default templates and extend them where possible, only changing what you need to customize.

This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.
TOC
    Version
    Become certified from home

    Become certified from home

    The life jacket for your team and your project

    The life jacket for your team and your project

    Version:

    Table of Contents

    • The recipe
      • Configure your Admin service
      • Create your customized template

    Symfony footer

    Avatar of Matthias Schmidt, a Symfony contributor

    Thanks Matthias Schmidt for being a Symfony contributor

    2 commits • 6 lines changed

    View all contributors that help us make Symfony

    Become a Symfony contributor

    Be an active part of the community and contribute ideas, code and bug fixes. Both experts and newcomers are welcome.

    Learn how to contribute

    Symfony™ is a trademark of Symfony SAS. All rights reserved.

    • What is Symfony?

      • What is Symfony?
      • Symfony at a Glance
      • Symfony Components
      • Symfony Releases
      • Security Policy
      • Logo & Screenshots
      • Trademark & Licenses
      • symfony1 Legacy
    • Learn Symfony

      • Symfony Docs
      • Symfony Book
      • Reference
      • Bundles
      • Best Practices
      • Training
      • eLearning Platform
      • Certification
    • Screencasts

      • Learn Symfony
      • Learn PHP
      • Learn JavaScript
      • Learn Drupal
      • Learn RESTful APIs
    • Community

      • Symfony Community
      • SymfonyConnect
      • Events & Meetups
      • Projects using Symfony
      • Contributors
      • Symfony Jobs
      • Backers
      • Code of Conduct
      • Downloads Stats
      • Support
    • Blog

      • All Blog Posts
      • A Week of Symfony
      • Case Studies
      • Cloud
      • Community
      • Conferences
      • Diversity
      • Living on the edge
      • Releases
      • Security Advisories
      • Symfony Insight
      • Twig
      • SensioLabs Blog
    • Services

      • SensioLabs services
      • Train developers
      • Manage your project quality
      • Improve your project performance
      • Host Symfony projects

      Powered by

    Follow Symfony