{% block contents %} conditional on a variable Django template inheritance

783 Views Asked by At

I am using Django's template inheritance from a base file called base.html.

All of my apps, except one, require that {% block contents %} {% endblock %} is present in that file, which is problematic.

I am trying to find away to make the inclusion {% block contents %} {% endblock %} conditional on a variable.

What I have so far tried in base.html:

    {% if some_variable %}
        {% block contents %} {% endblock %} 
    {% endif %}

But that doesn't seem to work.

I have also tried:

    {% if some_variable %}
        {% with 'base_block_content.html' as path %}
             {% include path %}
        {% endwith %}
    {% endif %}

base_block_content.html being simply:

''' {% block contents %} {% endblock %} '''

But that doesn't work either.

My only other option is to write a completely separate base.html for the one app, which doesn't quite fit in with the 'DRY' concept.

Can anyone suggest a way of doing this?

1

There are 1 best solutions below

0
On BEST ANSWER
{% block dynamic-block %}
{% if some_variable %}
   {% include 'path/index.html' %}
{% endif %}
{% endblock dynamic-lock %}