Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

how_to_customise_templates.rst 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. ==========================
  2. How to customise templates
  3. ==========================
  4. Assuming you want to use Oscar's templates in your project, there are two
  5. options. You don't have to though - you could write all your own templates if
  6. you like. If you do this, it's probably best to start with a straight copy of
  7. all of Oscar's templates so you know all the files that you need to
  8. re-implement.
  9. Anyway - here are the two options for customising.
  10. Method 1 - Forking
  11. ------------------
  12. One option is always just to fork the template into your local project so that
  13. it comes first in the include path.
  14. Say you want to customise ``base.html``. First you need a project-specific
  15. templates directory that comes first in the include path. You can set this up
  16. as so::
  17. import os
  18. location = lambda x: os.path.join(os.path.dirname(os.path.realpath(__file__)), '..', x)
  19. TEMPLATES = [
  20. {
  21. 'BACKEND': 'django.template.backends.django.DjangoTemplates',
  22. 'DIRS': [
  23. location('templates'), # templates directory of the project
  24. ],
  25. 'APP_DIRS': True,
  26. 'OPTIONS': {
  27. 'context_processors': [
  28. 'django.template.context_processors.debug',
  29. ...
  30. 'oscar.core.context_processors.metadata',
  31. ],
  32. },
  33. },
  34. ]
  35. Next copy Oscar's ``base.html`` into your templates directory and customise it
  36. to suit your needs.
  37. The downsides of this method are that it involves duplicating the file from
  38. Oscar in a way that breaks the link with upstream. Hence, changes to Oscar's
  39. ``base.html`` won't be picked up by your project as you will have your own
  40. version.
  41. Method 2 - Subclass parent but use same template path
  42. -----------------------------------------------------
  43. There is a trick you can perform whereby Oscar's templates can be accessed via
  44. two paths. This is outlined in the `Django wiki`_.
  45. .. _`Django wiki`: https://code.djangoproject.com/wiki/ExtendingTemplates
  46. This basically means you can have a ``base.html`` in your local templates folder
  47. that extends Oscar's ``base.html`` but only customises the blocks that it needs
  48. to.
  49. Hence to customise ``base.html``, you can have an implementation like::
  50. # base.html
  51. {% extends 'oscar/base.html' %}
  52. ...
  53. No real downsides to this one other than getting your front-end people to
  54. understand it.
  55. Overriding individual products partials
  56. ---------------------------------------
  57. Apart from overriding ``catalogue/partials/product.html`` to change the look
  58. for all products, you can also override it for individual product by placing
  59. templates in ``catalogue/detail-for-upc-%s.html`` or
  60. ``catalogue/detail-for-class-%s.html`` to customise look on product detail
  61. page and ``catalogue/partials/product/upc-%s.html`` or
  62. ``catalogue/partials/product/class-%s.html`` to tweak product rendering by
  63. ``{% render_product %}`` template tag , where ``%s`` is the product's UPC
  64. or class's slug, respectively.
  65. Example: Changing the analytics package
  66. ---------------------------------------
  67. Suppose you want to use an alternative analytics package to Google analytics.
  68. We can achieve this by overriding templates where the analytics urchin is loaded
  69. and called.
  70. The main template ``base.html`` has a 'tracking' block which includes a Google
  71. Analytics partial. We want to replace this with our own code. To do this,
  72. create a new ``base.html`` in your project that subclasses the original::
  73. # yourproject/templates/oscar/base.html
  74. {% extends 'oscar/base.html' %}
  75. {% block tracking %}
  76. <script type="javascript">
  77. ... [custom analytics here] ...
  78. </script>
  79. {% endblock %}
  80. Doing this will mean all templates that inherit from ``base.html`` will include
  81. your custom tracking.