Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django Includes - are they evil?

A friend and developer I respect recently advised that I avoid using 'Includes' in django templates. The sum of their argument was that includes are 'evil'.

I am having trouble understanding the logic; My novice opinion is that they are a great way to organise chunks of reusable html, and instead of having to edit html in multiple locations I can simply edit it in one when changes must be made.

What do all you geniuses think? Please provide some Pro's and Con's of using includes in Django templates

like image 750
just__matt Avatar asked Dec 19 '11 15:12

just__matt


People also ask

What {{ name }} means in a Django template?

What does {{ name }} this mean in Django Templates? {{ name }} will be the output. It will be displayed as name in HTML. The name will be replaced with values of Python variable.

Which characters are illegal in template variable name in Django?

Variable names consist of any combination of alphanumeric characters and the underscore ( "_" ) but may not start with an underscore, and may not be a number.

What is DTL in Django?

Django Template Language (DTL) is the primary way to generate output from a Django application. You can include DTL tags inside any HTML webpage. The basic DTL tag you can include in an HTML webpage is: 1. {% Tag %}

How define context Django?

A context is a variable name -> variable value mapping that is passed to a template. Context processors let you specify a number of variables that get set in each context automatically – without you having to specify the variables in each render() call.

Why is Django considered a bad movie?

Some reviews criticized the film for being too violent. The originally planned premiere of Django was postponed following the Sandy Hook school shooting on December 14, 2012. Thomas Frank criticized the film's use of violence as follows:

What is the use of Django template?

Django being a powerful Batteries included framework provides convenience to rendering data in a template. Django templates not only allow passing data from view to template, but also provides some limited features of programming such as variables, for loops, comments, extends, include etc.

Is Django Unchained a classic Western?

An entire issue of the academic journal Safundi was devoted to Django Unchained in " Django Unchained and the Global Western ," featuring scholars who contextualize Tarantino's film as a classic "western".

Why is Django Unchained more popular than 12 years a slave?

Samuel L. Jackson told Vogue Man that " Django Unchained was a harder and more detailed exploration of what the slavery experience was than 12 Years a Slave, but director Steve McQueen is an artist and since he's respected for making supposedly art films, it's held in higher esteem than Django, because that was basically a blaxploitation movie."


1 Answers

It seems a slightly odd opinion. Includes are a perfectly valid part of the template language, have been so since day 1 and have never AFAIK been recommended against or deprecated.

Your friend might be trying to say that you should rely more on template inheritance (which is kind of an opposite include). That's true as far as it goes - most of the time it's better to compose your templates of blocks that override or extend ones defined in parent templates. But there's definitely a use-case for includes.

The only other reason he might say that would be because of the added filesystem hit of loading include templates from disk. In which case, he's definitely off the mark - again, the template inheritance model which definitely is recommended would have exactly the same hit; and both can be avoided by using the optional caching filesystem loader.

like image 101
Daniel Roseman Avatar answered Sep 21 '22 19:09

Daniel Roseman