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 2.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. TEMPLATE_LOADERS = (
  18. 'django.template.loaders.filesystem.Loader',
  19. 'django.template.loaders.app_directories.Loader',
  20. )
  21. import os
  22. location = lambda x: os.path.join(os.path.dirname(os.path.realpath(__file__)), '..', x)
  23. TEMPLATE_DIRS = (
  24. location('templates'),
  25. )
  26. Next copy oscar's ``base.html`` into your templates directory and customise it
  27. to suit your needs.
  28. The downsides of this method are that it involves duplicating the file from
  29. oscar in a way that breaks the link with upstream. Hence, changes to oscar's
  30. ``base.html`` won't be picked up by your project as you will have your own
  31. version.
  32. Method 2 - Subclass parent but use same template path
  33. -----------------------------------------------------
  34. There is a trick you can perform whereby oscar's templates can be accessed via
  35. two paths. This is outlined in the `django wiki`_.
  36. .. _`django wiki`: https://code.djangoproject.com/wiki/ExtendingTemplates
  37. This basically means you can have a ``base.html`` in your local templates folder
  38. that extends oscar's ``base.html`` but only customises the blocks that it needs
  39. to.
  40. Oscar provides a helper variable to make this easy. First, set up your
  41. template configuration as so::
  42. TEMPLATE_LOADERS = (
  43. 'django.template.loaders.filesystem.Loader',
  44. 'django.template.loaders.app_directories.Loader',
  45. )
  46. import os
  47. location = lambda x: os.path.join(os.path.dirname(os.path.realpath(__file__)), '..', x)
  48. from oscar import OSCAR_PARENT_TEMPLATE_DIR
  49. TEMPLATE_DIRS = (
  50. location('templates'),
  51. OSCAR_PARENT_TEMPLATE_DIR,
  52. )
  53. The ``OSCAR_PARENT_TEMPLATE_DIR`` points to the directory above oscar's normal
  54. templates directory. This means that ``path/to/oscar/template.html`` can also
  55. be reached via ``templates/path/to/oscar/template.html``.
  56. Hence to customise ``base.html``, you can have an implementation like::
  57. # base.html
  58. {% extends 'templates/base.html' %}
  59. ...
  60. No real downsides to this one other than getting your front-end people to
  61. understand it.