Saturday, July 1, 2017

Django - 027 - Philosophies and Limitations

First and foremost, the limitations to the DTL (Django Template Language) are intentional.

Django was developed in the high volume, ever-changing environment of an online newsroom. The original creators of Django had a very definite set of philosophies in creating the DTL.

These philosophies remain core to Django today. They are:

  1. Separate Logic from presentation
  2. Discourage redundancy
  3. Be decoupled from HTML
  4. XML is bad
  5. Assume designer competence
  6. Treat whitespace obviously
  7. Don't invent a programming language
  8. Ensure safety and security
  9. Extensible


Following is the explanation for this:

1. Separate Logic from Presentation:
A template system is a tool that controls presentation and presentation related logic and that is it. The template system should not support functionality that goes beyond this basic goal.

2. Discourage Redundancy:
The majority of dynamic websites use some sort of common site-wide design - a common header, footer, navigation bar and so on. 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 should not be designed so that it only outputs HTML. It should be equally good at generating other text based formats, or just 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 - an incurs an unacceptable level of overhead in template processing.

5. Assume Designer comptance:
The template system should not be designed so that templates necessarily are displayed nicely in WYSIWYG editors such as Dreamweaver. That is too severe of a limitation and wouldn't allow the syntax to be as nice as it is.

Django expects template authors are comfortable editing HTML directly.

6. Treat whitespace obviously:
The template system should not do magic things with whitespace. If a template includes whitespace, the system should treat the whitespace as it treats text-just display it. Any whitespace that is not in a template tag should be displayed.

7. Do not invent a programming language:
The template system intentionally does not allow the following:
Assignment to variables.
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, it is essential for making presentation related decisions.

The Django template system recognizes that templates are most often written by designers, not programmers, and therefore should not assume Python knowledge.

8. Safety and Security:
The template system, out of the box, should forbid the inclusion of malicious code - such as commands that delete database records. This is another reason the template system does not allow arbitrary 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.

When the pressure is on to GetStuffDone, and you have both designers and programmers trying to communicate and get all of the last minute tasks done, Django just 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 very quickly why Django really is the framework for perfectionists with deadlines.

With all this in mind, Django is flexible - it does not require you to use the DTL. More than any other component of web applications, template syntax is highly subjective, and programmers opinions vary wildly. The fact that Python alone has dozens, if not hundreds, of open-source template-language implementations supports this point. Each was likely created because its developer deemed all existing template-languages inadequate.

Because Django is intended to be a full-stack web framework that provides all the pieces necessary for web developers to be productive, most times it is more convenient to use the DTL, but it is not a strict requirement in any sense.


No comments:

Post a Comment