templates.rst 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. # default values of actions templates, they should extend global templates
  11. list: SonataAdminBundle:CRUD:list.html.twig
  12. show: SonataAdminBundle:CRUD:show.html.twig
  13. edit: SonataAdminBundle:CRUD:edit.html.twig
  14. history: SonataAdminBundle:CRUD:history.html.twig
  15. preview: SonataAdminBundle:CRUD:preview.html.twig
  16. delete: SonataAdminBundle:CRUD:delete.html.twig
  17. # default values of helper templates
  18. short_object_description: SonataAdminBundle:Helper:short-object-description.html.twig
  19. # default values of block templates, they should extend the base_block template
  20. list_block: SonataAdminBundle:Block:block_admin_list.html.twig
  21. Usage of each template :
  22. * layout : base layout used by the dashboard and an admin class
  23. * ajax : default layout used when an ajax request is performed
  24. * dashboard: default layout used at the dashboard
  25. * list : the template to use for the list action
  26. * show : the template to use for the show action
  27. * edit : the template to use for the edit and create action
  28. * history : the template to use for the history / audit action
  29. * list_block : the template used for the list of admin blocks on the dashboard
  30. * preview : the template to use for previewing an edit / create action
  31. * short_object_description: used to represent the entity in one-to-one/many-to-one relations
  32. * delete: the template to use for the delete action
  33. The default values will be set only if the ``Admin::setTemplates`` is not called by the Container.
  34. You can easily extend the provided templates in your own and customize only the blocks you need to change:
  35. .. code-block:: jinja
  36. {% extends 'SonataAdminBundle:CRUD:edit.html.twig' %}
  37. {# Acme/MyBundle/Ressources/view/my-custom-edit.html.twig #}
  38. {% block title %}
  39. {{ "My title"|trans }}
  40. {% endblock%}
  41. {% block actions %}
  42. <div class="sonata-actions">
  43. <ul>
  44. {% if admin.hasroute('list') and admin.isGranted('LIST')%}
  45. <li class="btn sonata-action-element"><a href="{{ admin.generateUrl('list') }}">{% trans from 'SonataAdminBundle' %}link_action_list{% endtrans %}</a></li>
  46. {% endif %}
  47. </ul>
  48. </div>
  49. {% endblock %}
  50. .. code-block:: php
  51. <?php // MyAdmin.php
  52. public function getTemplate($name)
  53. {
  54. switch ($name) {
  55. case 'edit':
  56. return 'AcmeMyBundle::my-custom-edit.html.twig';
  57. break;
  58. default:
  59. return parent::getTemplate($name);
  60. break;
  61. }
  62. }