- Template Design Philosophy
- 1. Separate logic from presentation
- 2. Discourage redundancy
- 3. Be decoupled from HTML
- 4. XML should not be used for template languages
- 5. Assume designer competence
- 6. Treat whitespace obviously
- 7. Don’t invent a programming language
- 8. Safety and security
- 9. Extensibility
- DTL Philosophy—Concluding Thoughts
Template Design Philosophy
Django’s approach to web design is simple—keep Django logic and code separate from design. It’s essential to understand that Django’s templates are not Python code embedded into HTML; it’s not possible to execute Python code in a Django template.
This means a designer can create a complete frontend (HTML, CSS, imagery and user interaction) without having to write a single line of Python or Django code. A designer need only leave HTML comments in the template for a programmer to replace with template tags—plain text markup tags defined by the Django Template Language (DTL).
While the DTL is similar to other template languages that embed template code in plain HTML, the original creators of Django had a specific set of philosophies in mind when creating the DTL. These philosophies remain core to Django today. They are:
- Separate logic from presentation
- Discourage redundancy
- Be decoupled from HTML
- XML is bad
- Assume designer competence
- Treat whitespace obviously
- Don’t invent a programming language
- Ensure safety and security
- Extensible
1. Separate logic from presentation
A template system is a tool to control presentation and presentation-related logic—and that’s it. The template system shouldn’t support functionality that goes beyond this primary goal.
2. Discourage redundancy
Most dynamic web sites use many common site-wide design elements—a common header, footer, navigation bar, etc. The Django template system should make it easy to store those elements in a single place, eliminating duplicate code. This is the philosophy behind template inheritance.
3. Be decoupled from HTML
The template system shouldn’t just output HTML. It should be equally good at generating other text-based formats, or plain text.
4. XML should not be used for template languages
Using an XML engine to parse templates introduces a whole new world of human error in editing templates—and incurs an unacceptable level of overhead in template processing.
5. Assume designer competence
The template system shouldn’t be designed so templates display nicely in WYSIWYG editors such as Dreamweaver. That is too severe of a limitation and wouldn’t allow the syntax to be as concise as it is.
Django expects template authors to be comfortable editing HTML directly.
6. Treat whitespace obviously
The template system shouldn’t do magic things with whitespace. If a template includes whitespace, the system should treat it as it treats text— display it. Any whitespace that’s not in a template tag should be displayed.
7. Don’t invent a programming language
The template system intentionally doesn’t allow
- assignment to variables; or
- advanced logic.
The goal is not to invent a programming language. The goal is to offer just enough programming-esque functionality, such as branching and looping, essential for making presentation-related decisions.
The Django template system recognizes designers, not programmers, usually create templates. Therefore, the template language should not assume Python knowledge.
8. Safety and security
The template system should forbid the inclusion of malicious code—such as commands that delete database records. This is another reason why the template system doesn’t allow arbitrary execution of Python code.
9. Extensibility
The template system should recognize that advanced template authors may want to extend its technology. This is the philosophy behind custom template tags and filters.
DTL Philosophy—Concluding Thoughts
Having worked with many templating systems myself over the years, I whole-heartedly endorse this approach—the philosophy and design behind the DTL is one of the major advantages of the Django framework.
When the pressure is on to Get Stuff Done, and you have both designers and programmers trying to communicate and get all last-minute tasks done, Django gets out of the way and lets each team concentrate on what they are good at.
Once you have found this out for yourself through real-life practice, you will find out quickly why Django really is the “framework for perfectionists with deadlines”.
Remember though, template syntax is highly subjective, and programmers’ opinions vary wildly. Python alone has dozens of open source template-language implementations, each created because its developer deemed all existing template languages inadequate.
With this in mind, Django is flexible—it does not require you to use the DTL. All versions of Django since Django 1.8, ship with the popular Jinja2 template engine along with the DTL to provide developers with options.
Because Django is intended to be a full-stack web framework providing all the pieces necessary for web developers to be productive, most times it’s more convenient to use the DTL, but it’s not a requirement.