recipe_row_templates.rst 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. Row templates
  2. =============
  3. Since Sonata-2.2 it is possible to define a template per row for the list action.
  4. The default template is a standard table but there are circumstances where this
  5. type of layout might not be suitable. By defining a custom template for the row,
  6. you can tweak the layout into something like this:
  7. .. figure:: ./../images/sonata_inline_row.png
  8. :align: center
  9. :alt: Inline Row from the SonataNewsBundle
  10. :width: 700px
  11. The recipe
  12. ----------
  13. Configure your Admin service
  14. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  15. The configuration takes place in the DIC by calling the ``setTemplates`` method.
  16. Two template keys need to be set:
  17. - ``inner_list_row``: The template for the row, which you will customize. Often
  18. you will want this to extend ``SonataAdminBundle:CRUD:base_list_flat_inner_row.html.twig``
  19. - ``base_list_field``: The base template for the cell, the default of
  20. ``SonataAdminBundle:CRUD:base_list_flat_field.html.twig`` is suitable for most
  21. cases but it can be customized if required.
  22. .. configuration-block::
  23. .. code-block:: xml
  24. <service id="sonata.admin.comment" class="%sonata.admin.comment.class%">
  25. <tag name="sonata.admin" manager_type="orm" group="sonata_blog" label="comments"
  26. label_catalogue="%sonata.admin.comment.translation_domain%"
  27. label_translator_strategy="sonata.admin.label.strategy.underscore" />
  28. <argument />
  29. <argument>%sonata.admin.comment.entity%</argument>
  30. <argument>%sonata.admin.comment.controller%</argument>
  31. <call method="setTemplates">
  32. <argument type="collection">
  33. <argument key="inner_list_row">
  34. AppBundle:Admin:inner_row_comment.html.twig
  35. </argument>
  36. <argument key="base_list_field">
  37. SonataAdminBundle:CRUD:base_list_flat_field.html.twig
  38. </argument>
  39. </argument>
  40. </call>
  41. </service>
  42. Create your customized template
  43. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  44. Once the templates are defined, create the template to render the row:
  45. .. code-block:: jinja
  46. {# AppBundle:Admin:inner_row_comment.html.twig #}
  47. {# Extend the default template, which provides batch and action cells #}
  48. {# as well as the valid colspan computation #}
  49. {% extends 'SonataAdminBundle:CRUD:base_list_flat_inner_row.html.twig' %}
  50. {% block row %}
  51. {# you can use fields defined in the the Admin class #}
  52. {{ object|render_list_element(admin.list['name']) }} -
  53. {{ object|render_list_element(admin.list['url']) }} -
  54. {{ object|render_list_element(admin.list['email']) }} <br />
  55. <small>
  56. {# or you can use the object variable to render a property #}
  57. {{ object.message }}
  58. </small>
  59. {% endblock %}
  60. While this feature is nice to generate a rich list, it is also very easy to
  61. break the layout and admin features such as batch and object actions. It is
  62. best to familiarise yourself with the default templates and extend them where
  63. possible, only changing what you need to customize.