[Answer]-Nested block inside if condition django

1👍

There’s two ways of handling it. If you want it to be in a single template, then you can do this:

{% block a %}{% if var = '1' %}active{% else %}{{ block.super }}{%endif%}{%endblock%}
{% block b %}{% if var = '2' %}active{% else %}{{ block.super }}{%endif%}{%endblock%}
{% block c %}{% if var = '3' %}active{% else %}{{ block.super }}{%endif%}{%endblock%}

The other way would be to create multiple templates and determine which one to use based on var.

In var1.html:

{% block a %}active{% endblock %}

In var2.html:

{% block b %}active{% endblock %}

In var3.html:

{% block c %}active{% endblock %}

You could also have three different templates that you extend with the template that you’re working in. You’d have your three variations with the different active element. Then in your template that would extend one of them, you’d do:

{% extend variable_name %}

Leave a comment