'using Django "include" template tag as nested - side effects?

Does anyone know or have any experience about bad side effects of using "nested include tags"? (I mean including a template file which itself includes another template)

For example:

file x.html:

{% include "z.html" %}

file y.html:

{% inclide "x.html" %}



Solution 1:[1]

Bad side effects are messed up CSS rules and some headache while debugging. It is generally better to keep them rather flat.

x.html:

{% extends "_base.html" %}

{% block main_content %}
    <h1>I am X</h1>

    {% include "includes/z.html" %}
    
{% endblock main_content %}

y.html:

{% block main_content %}
    <h1>I am Y</h1>

    {% include "x.html" %}
    
{% endblock main_content %}

z.html:

<h2>I am Z</h2>

1 2

Check the code: https://github.com/almazkun/templating

Solution 2:[2]

I think you are confusing include and extends

Logically, extending a template when used in the likes of a base.html is great, and if you design your templates in a way which allows you to extend certain base html files for certain sections of your site. This is generally when they have shared snippets of html, like the main css block, meta block, script block etc..

You can extend n number of times.

Where you are getting confused with include, is that this is more for including snippets of code which are additive or sort of "drop-in" and great examples would be sidebars, navbars, and even custom css or javascript!

The question is, do you want to shared html snippets, or do you want to add extra snippets to certain sections but not others?

If its the latter, you'll want include, and for the former, you'll want extends.

Edit #1: You have asked for clarity regarding the drawbacks of the two aforementioned template tags. As I sit here and ponder that question, I cant think of any significant disadvantages of using either include or extends providing that you have used them where they should be used. If you have used an include, where actually you should have used extended the template, you may find, as is described in someone else's answer, that css rules may not be applied in the way you would expect them to be.

Solution 3:[3]

The question is about nested include tags and the answers show that "there is no actual problem if you know what you expect and how to handle it".

There is a paragraph in the docs about the include tag that sets some limits, nonetheless:

The include tag should be considered as an implementation of “render this subtemplate and include the HTML”, not as “parse this subtemplate and include its contents as if it were part of the parent”. This means that there is no shared state between included templates – each include is a completely independent rendering process.

Solution 4:[4]

No problem! It's usual to use inheritance in Django templates. You can make a template with some blocks and override blocks in children templates and children of children can override or use superblock content.

Solution 5:[5]

I didn't have any issues chaining include. You might have issues if have extend or blocks with similar names in the included template.

Solution 6:[6]

There are no issues when you include a number of templates.

for example base.html

<!doctype html>
<html lang="en">
  <head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <!-- Bootstrap CSS -->
    <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">

    {% block title %}{% endblock %}
  </head>
  <body>
    {% block content %}
    {% endblock %}

    <!-- Optional JavaScript; choose one of the two! -->

    <!-- Option 1: Bootstrap Bundle with Popper -->
    <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p" crossorigin="anonymous"></script>

    <!-- Option 2: Separate Popper and Bootstrap JS -->
    <!--
    <script src="https://cdn.jsdelivr.net/npm/@popperjs/[email protected]/dist/umd/popper.min.js" integrity="sha384-7+zCNj/IqJ95wo16oMtfsKbZ9ccEh31eOz1HGyDuCQ6wgnyJNSYdrPa03rtR1zdB" crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.min.js" integrity="sha384-QJHtvGhmr9XOIpI6YVutG+2QOK9T+ZnN4kzFN1RtK3zEFEIsxhlmWl5/YESvpZ13" crossorigin="anonymous"></script>
    -->
  </body>
</html>

a.html

<h1>I am from A</h1>

b.html

<h2>I am from B</h2>

c.html

{% extends 'base.html' %}
{% load static %}
{% block title %}<title>Welcome in C!</title>{% endblock %}
<h3>I am from C . I am mother of A and B </h3>
{% block content %}
{% include 'a.html' %}
{% include 'b.html' %}
{% endblock %}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Yevgeniy Kosmak
Solution 2
Solution 3
Solution 4 mrash
Solution 5 Jon Mathew
Solution 6 Akash Nagtilak