templates.rst 2.9 KB

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