Search code examples
djangonestedtagsincludejinja2

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

  • 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