You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

how_to_customise_templates.rst 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. Oscar provides a helper variable to make this easy. First, set up your
  50. template configuration as so::
  51. import os
  52. from oscar import OSCAR_MAIN_TEMPLATE_DIR
  53. location = lambda x: os.path.join(os.path.dirname(os.path.realpath(__file__)), '..', x)
  54. TEMPLATES = [
  55. {
  56. 'BACKEND': 'django.template.backends.django.DjangoTemplates',
  57. 'DIRS': [
  58. location('templates'), # templates directory of the project
  59. OSCAR_MAIN_TEMPLATE_DIR,
  60. ],
  61. 'APP_DIRS': True,
  62. 'OPTIONS': {
  63. 'context_processors': [
  64. 'django.template.context_processors.debug',
  65. ...
  66. 'oscar.core.context_processors.metadata',
  67. ],
  68. },
  69. },
  70. ]
  71. The ``OSCAR_MAIN_TEMPLATE_DIR`` points to the directory above Oscar's normal
  72. templates directory. This means that ``path/to/oscar/template.html`` can also
  73. be reached via ``templates/path/to/oscar/template.html``.
  74. Hence to customise ``base.html``, you can have an implementation like::
  75. # base.html
  76. {% extends 'oscar/base.html' %}
  77. ...
  78. No real downsides to this one other than getting your front-end people to
  79. understand it.
  80. Overriding individual products partials
  81. ---------------------------------------
  82. Apart from overriding ``catalogue/partials/product.html`` to change the look
  83. for all products, you can also override it for individual product by placing
  84. templates in ``catalogue/detail-for-upc-%s.html`` or
  85. ``catalogue/detail-for-class-%s.html`` to customize look on product detail
  86. page and ``catalogue/partials/product/upc-%s.html`` or
  87. ``catalogue/partials/product/class-%s.html`` to tweak product rendering by
  88. ``{% render_product %}`` template tag , where ``%s`` is the product's UPC
  89. or class's slug, respectively.
  90. Example: Changing the analytics package
  91. ---------------------------------------
  92. Suppose you want to use an alternative analytics package to Google analytics.
  93. We can achieve this by overriding templates where the analytics urchin is loaded
  94. and called.
  95. The main template ``base.html`` has a 'tracking' block which includes a Google
  96. Analytics partial. We want to replace this with our own code. To do this,
  97. create a new ``base.html`` in your project that subclasses the original::
  98. # yourproject/templates/base.html
  99. {% extends 'oscar/base.html' %}
  100. {% block tracking %}
  101. <script type="javascript">
  102. ... [custom analytics here] ...
  103. </script>
  104. {% endblock %}
  105. Doing this will mean all templates that inherit from ``base.html`` will include
  106. your custom tracking.