templates.rst 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. Templates
  2. =========
  3. By default, an Admin class uses a set of templates, it is possible to tweak the default values by editing the configuration
  4. .. code-block:: yaml
  5. sonata_admin:
  6. templates:
  7. # default global templates
  8. layout: SonataAdminBundle::standard_layout.html.twig
  9. ajax: SonataAdminBundle::ajax_layout.html.twig
  10. dashboard: SonataAdminBundle:Core:dashboard.html.twig
  11. # default values of actions templates, they should extend global templates
  12. list: SonataAdminBundle:CRUD:list.html.twig
  13. show: SonataAdminBundle:CRUD:show.html.twig
  14. edit: SonataAdminBundle:CRUD:edit.html.twig
  15. history: SonataAdminBundle:CRUD:history.html.twig
  16. preview: SonataAdminBundle:CRUD:preview.html.twig
  17. delete: SonataAdminBundle:CRUD:delete.html.twig
  18. batch: SonataAdminBundle:CRUD:list__batch.html.twig
  19. batch_confirmation: SonataAdminBundle:CRUD:batch_confirmation.html.twig
  20. # list related templates
  21. inner_list_row: SonataAdminBundle:CRUD:list_inner_row.html.twig
  22. base_list_field: SonataAdminBundle:CRUD:base_list_field.html.twig
  23. # default values of helper templates
  24. short_object_description: SonataAdminBundle:Helper:short-object-description.html.twig
  25. # default values of block templates, they should extend the base_block template
  26. list_block: SonataAdminBundle:Block:block_admin_list.html.twig
  27. # default values of pager templates
  28. pager_links: SonataAdminBundle:Pager:links.html.twig
  29. pager_results: SonataAdminBundle:Pager:results.html.twig
  30. Usage of each template :
  31. * layout : base layout used by the dashboard and an admin class
  32. * ajax : default layout used when an ajax request is performed
  33. * dashboard: default layout used at the dashboard
  34. * list : the template to use for the list action
  35. * show : the template to use for the show action
  36. * edit : the template to use for the edit and create action
  37. * history : the template to use for the history / audit action
  38. * list_block : the template used for the list of admin blocks on the dashboard
  39. * preview : the template to use for previewing an edit / create action
  40. * short_object_description: used to represent the entity in one-to-one/many-to-one relations
  41. * delete: the template to use for the delete action
  42. * inner_list_row: the template to render a list row
  43. * pager_links: the template to render the first, previous, next and last page links
  44. * pager_results: the template to render the amount of pages, number of results and items per page
  45. The default values will be set only if the ``Admin::setTemplates`` is not called by the Container.
  46. You can easily extend the provided templates in your own template and customize only the blocks you need to change:
  47. .. code-block:: jinja
  48. {% extends 'SonataAdminBundle:CRUD:edit.html.twig' %}
  49. {# Acme/MyBundle/Resources/view/my-custom-edit.html.twig #}
  50. {% block title %}
  51. {{ "My title"|trans }}
  52. {% endblock%}
  53. {% block actions %}
  54. <div class="sonata-actions">
  55. <ul>
  56. {% if admin.hasroute('list') and admin.isGranted('LIST')%}
  57. <li class="btn sonata-action-element"><a href="{{ admin.generateUrl('list') }}">{{ 'link_action_list'|trans({}, 'SonataAdminBundle') }}</a></li>
  58. {% endif %}
  59. </ul>
  60. </div>
  61. {% endblock %}
  62. .. code-block:: php
  63. <?php // MyAdmin.php
  64. public function getTemplate($name)
  65. {
  66. switch ($name) {
  67. case 'edit':
  68. return 'AcmeMyBundle::my-custom-edit.html.twig';
  69. break;
  70. default:
  71. return parent::getTemplate($name);
  72. break;
  73. }
  74. }
  75. Row Template
  76. ------------
  77. From 2.2, it is possible to define a template per row for the list action, the default one is a standard table. However,
  78. depends on the data the table layout might not be suitable. So by defining a custom template for the row, it will be
  79. possible to tweak the layout as:
  80. .. figure:: ./../images/sonata_inline_row.png
  81. :align: center
  82. :alt: Inline Row from the SonataNewsBundle
  83. :width: 700px
  84. How to use it
  85. ~~~~~~~~~~~~~
  86. The configuration takes place in the DIC by calling the ``setTemplates`` method. Two template keys need to be set:
  87. - ``inner_list_row`` : The template for the row, this one need to be customized
  88. - ``base_list_field`` : The base template for the cell, the ``SonataAdminBundle:CRUD:base_list_flat_field.html.twig``
  89. is suitable for most cases, however advance use might want to change it.
  90. .. code-block:: xml
  91. <service id="sonata.news.admin.comment" class="%sonata.news.admin.comment.class%">
  92. <tag name="sonata.admin" manager_type="orm" group="sonata_blog" label="comments" label_catalogue="%sonata.news.admin.comment.translation_domain%" label_translator_strategy="sonata.admin.label.strategy.underscore" />
  93. <argument />
  94. <argument>%sonata.news.admin.comment.entity%</argument>
  95. <argument>%sonata.news.admin.comment.controller%</argument>
  96. <call method="setTemplates">
  97. <argument type="collection">
  98. <argument key="inner_list_row">SonataNewsBundle:Admin:inner_row_comment.html.twig</argument>
  99. <argument key="base_list_field">SonataAdminBundle:CRUD:base_list_flat_field.html.twig</argument>
  100. </argument>
  101. </call>
  102. </service>
  103. Once the template is set, edit the template ``SonataNewsBundle:Admin:inner_row_comment.html.twig``
  104. .. code-block:: jinja
  105. {# The default template which provides batch and action cells, with the valid colspan computation #}
  106. {% extends 'SonataAdminBundle:CRUD:base_list_flat_inner_row.html.twig' %}
  107. {% block row %}
  108. {# use field define in the the Admin class #}
  109. {{ object|render_list_element(admin.list['name']) }} -
  110. {{ object|render_list_element(admin.list['url']) }} -
  111. {{ object|render_list_element(admin.list['email']) }} <br />
  112. <small>
  113. {# or you can use the object variable to render a property #}
  114. {{ object.message }}
  115. </small>
  116. {% endblock %}
  117. While this feature is nice to generate rich list, it is also very easy to break the layout and admin features: batch and actions.