templates.rst 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. Templates
  2. =========
  3. ``SoantaAdminBundle`` comes with a significant amount of ``twig`` files used to display the
  4. different parts of each ``Admin`` action's page. If you read the ``Templates`` part of the :doc:`architecture` section of this guide, you should know by now how these are organized in
  5. the ``views`` folder. If you haven't, now would be a good time to do it.
  6. Besides these, some other views files are included from the storage layer. As their content and
  7. structure are specific to each implementation, they are not discussed here, but it's important
  8. that you keep in mind that they exist and are as relevant as the view files included
  9. directly in ``SonataAdminBundle``.
  10. Global Templates
  11. ----------------
  12. ``SonataAdminBundle`` views are implemented using ``twig`` files, and take full advantage of its
  13. inheritance capabilities. As such, even the most simple page is actually rendered using many
  14. different ``twig`` files. At the end of that ``twig`` inheritance hierarchy is always one of two files:
  15. * layout: SonataAdminBundle::standard_layout.html.twig
  16. * ajax: SonataAdminBundle::ajax_layout.html.twig
  17. As you might have guessed from their names, the first is used in 'standard' request and the other
  18. for AJAX calls. The ``SonataAdminBundle::standard_layout.html.twig`` contains several elements which
  19. exist across the whole page, like the logo, title, upper menu and menu. It also includes the base CSS
  20. and JavaScript files and libraries used across the whole administration section. The AJAX template
  21. doesn't include any of these elements.
  22. Dashboard Template
  23. ------------------
  24. The template used for rendering the dashboard can also be configured. See the :doc:`dashboard` page
  25. for more information
  26. CRUDController Actions Templates
  27. --------------------------------
  28. As seen before, the ``CRUDController`` has several actions that allow you to manipulate your
  29. model instances. Each of those actions uses a specific template file to render its content.
  30. By default, ``SonataAdminBundle`` uses the following templates for their matching action:
  31. * list: SonataAdminBundle:CRUD:list.html.twig
  32. * show: SonataAdminBundle:CRUD:show.html.twig
  33. * edit: SonataAdminBundle:CRUD:edit.html.twig
  34. * history: SonataAdminBundle:CRUD:history.html.twig
  35. * preview: SonataAdminBundle:CRUD:preview.html.twig
  36. * delete: SonataAdminBundle:CRUD:delete.html.twig
  37. * batch_confirmation: SonataAdminBundle:CRUD:batch_confirmation.html.twig
  38. * acl: SonataAdminBundle:CRUD:acl.html.twig
  39. Notice that all these templates extend other templates, and some do only that. This inheritance
  40. architecture is designed to help you easily make customizations by extending these templates
  41. in your own bundle, rather than rewriting everything.
  42. If you look closely, all of these templates ultimately extend the ``base_template`` variable that's
  43. passed from the controller. This variable will always take the value of one of the above mentioned
  44. global templates, and this is how changes made to those files affect all the ``SonataAdminBundle``
  45. interface.
  46. Row Templates
  47. -------------
  48. It is possible to completely change how each row of results is rendered in the
  49. list view, by customizing the ``inner_list_row`` and ``base_list_field`` templates.
  50. For more information about this, see the :doc:`recipe_row_templates`
  51. cookbook entry.
  52. Other Templates
  53. ---------------
  54. There are several other templates that can be customized, enabling you to fine-tune
  55. ``SonataAdminBundle``:
  56. * user_block: customizes the Twig block rendered by default in the top right corner of the admin interface, containing user information. Empty by defautl, see ``SonataUserBundle`` for a real example.
  57. * history_revision_timestamp: customizes the way timestamps are rendered when using history related actions.
  58. * action: a generic template you can use for your custom actions
  59. * short_object_description: used by the ``getShortObjectDescriptionAction`` action from the ``HelperController``, this template displays a small description of a model instance.
  60. * list_block: the template used to render the dashboard's admin mapping lists. More info on the :doc:`dashboard` page.
  61. * batch: template used to render the checkboxes that precede each instance on list views.
  62. * select: when loading list views as part of sonata_admin form types, this template is used to create a button that allows you to select the matching line.
  63. * pager_links: renders the list of pages displayed at the end of the list view (when more than one page exists)
  64. * pager_results: renders the dropdown that lets you choose the number of elements per page on list views
  65. Configuring templates
  66. ---------------------
  67. Like said before, the main goal of this template structure is to make it easy for you
  68. to customize the ones you need. You can simply extend the ones you want in your own bundle,
  69. and tell ``SonataAdminBundle`` to use your templates instead of the default ones. You can do so
  70. in several ways.
  71. You can specify your templates in the config.yml file, like so:
  72. .. configuration-block::
  73. .. code-block:: yaml
  74. sonata_admin:
  75. templates:
  76. layout: SonataAdminBundle::standard_layout.html.twig
  77. ajax: SonataAdminBundle::ajax_layout.html.twig
  78. list: SonataAdminBundle:CRUD:list.html.twig
  79. show: SonataAdminBundle:CRUD:show.html.twig
  80. edit: SonataAdminBundle:CRUD:edit.html.twig
  81. history: SonataAdminBundle:CRUD:history.html.twig
  82. preview: SonataAdminBundle:CRUD:preview.html.twig
  83. delete: SonataAdminBundle:CRUD:delete.html.twig
  84. batch: SonataAdminBundle:CRUD:list__batch.html.twig
  85. acl: SonataAdminBundle:CRUD:acl.html.twig
  86. action: SonataAdminBundle:CRUD:action.html.twig
  87. select: SonataAdminBundle:CRUD:list__select.html.twig
  88. dashboard: SonataAdminBundle:Core:dashboard.html.twig
  89. search: SonataAdminBundle:Core:search.html.twig
  90. batch_confirmation: SonataAdminBundle:CRUD:batch_confirmation.html.twig
  91. inner_list_row: SonataAdminBundle:CRUD:list_inner_row.html.twig
  92. base_list_field: SonataAdminBundle:CRUD:base_list_field.html.twig
  93. inner_list_row: SonataAdminBundle:CRUD:list_inner_row.html.twig
  94. base_list_field: SonataAdminBundle:CRUD:base_list_field.html.twig
  95. list_block: SonataAdminBundle:Block:block_admin_list.html.twig
  96. user_block: SonataAdminBundle:Core:user_block.html.twig
  97. pager_links: SonataAdminBundle:Pager:links.html.twig
  98. pager_results: SonataAdminBundle:Pager:results.html.twig
  99. history_revision_timestamp: SonataAdminBundle:CRUD:history_revision_timestamp.html.twig
  100. short_object_description: SonataAdminBundle:Helper:short-object-description.html.twig
  101. search_result_block: SonataAdminBundle:Block:block_search_result.html.twig
  102. Notice that this is a global change, meaning it will affect all model mappings automatically,
  103. both for ``Admin`` mappings defined by you and by other bundles.
  104. If you wish, you can specify custom templates on a per ``Admin`` mapping basis. Internally,
  105. the ``CRUDController`` fetches this information from the ``Admin`` class instance, so you can
  106. specify the templates to use in the ``Admin`` service definition:
  107. .. configuration-block::
  108. .. code-block:: xml
  109. <service id="sonata.admin.post" class="Acme\DemoBundle\Admin\PostAdmin">
  110. <tag name="sonata.admin" manager_type="orm" group="Content" label="Post"/>
  111. <argument />
  112. <argument>Acme\DemoBundle\Entity\Post</argument>
  113. <argument />
  114. <call method="setTemplate">
  115. <argument>edit</argument>
  116. <argument>AcmeDemoBundle:PostAdmin:edit.html.twig</argument>
  117. </call>
  118. </service>
  119. .. code-block:: yaml
  120. services:
  121. sonata.admin.post:
  122. class: Acme\DemoBundle\Admin\PostAdmin
  123. tags:
  124. - { name: sonata.admin, manager_type: orm, group: "Content", label: "Post" }
  125. arguments:
  126. - ~
  127. - Acme\DemoBundle\Entity\Post
  128. - ~
  129. calls:
  130. - [ setTemplate, [edit, AcmeDemoBundle:PostAdmin:edit.html.twig]]
  131. .. note::
  132. A ``setTemplates(array $templates)`` (notice the plural) function also exists, that allows
  133. you to set multiple templates at once. Notice that, if used outside of the service definition
  134. context, ``setTemplates(array $templates)`` will replace the whole template list for that
  135. ``Admin`` class, meaning you have to explicitly pass the full template list in the
  136. ``$templates`` argument.
  137. Changes made using the ``setTemplate()`` and ``setTemplates()`` functions override the customizations
  138. made in the configuration file, so you can specify a global custom template and then override that
  139. customization on a specific ``Admin`` class.