So I’m building an application using django. A few people in my team think I should be using includes in my templates rather than following the blocks/extends paradigm. They argue that:
- Inheritance is too much logic for future maintainers
- Includes are much easier to follow
- We shouldn’t limit future maintenance to “django programmers”
I’d really rather not butcher my application’s templates, so I’m trying to come up with some convincing reasons (for non-django users) as to why inheritance really is the way forward. I’d love to hear some more views on this.
A few reasons I’ve come up with already:
-
Much more DRY. Ensures that boilerplate html structures only need to be defined in one place; much less risk of templates getting out of sync
-
Keeps content which belongs in templates, in templates, and out of views; for example:
<title>{% block title %}My Website{% endblock %}</title>vs
<title>{{ pagetitle }}</title>This gives the front-end maintainer much more control over the front-end, without having to delve into back-end views
-
Front end coders already have to be familiar with the concept of inheritance of extending from a base / overriding defaults; consider the following css:
.nav { color: blue; } .nav.active { color: purple; }
Any good reasoning you can throw into the mix?
Using inheritance makes it easier (at least IMHO) to create a master base.html template and let all of the other pages
extendsit. This leaves all layout/styling to be specified in that one file, and the rest simply use tags withidandclassin a standardized way.It is good to remember that the argument to
extends(as well asinclude) can be a variable. Ourtemplates/base.htmlis one line:where
base_templateis set in our master context processor. This, together with a simple admin widget that changessession.base_template, has allowed our designer to create multiple layouts and switch between them with a single click. She absolutely loves it.