SilentDev
SilentDev

Reputation: 22777

Django execute block tag inside 'if' statement in template only if 'if' statement is true

I want to execute a block tag only if an if statement is true. Here is what I mean. This is my template:

{% extends "homePageBase.html" %}

{% if not blogPage %}
    {% block isBlogFalse %} notBlogPage {% endblock %}
{% else %}
    {% block isBlogTrue %} blogPage {% endblock %}
{% endif %}

But both the block tags get executed. Is there any way for the block tags to only get executed if the if / else statements are true? Any way around this issue or any way of accomplishing this task?

Upvotes: 4

Views: 1754

Answers (1)

PxlBuzzard
PxlBuzzard

Reputation: 25

One answer to the problem can be found at https://stackoverflow.com/a/18638131/573392. To modify it for your situation, it would look like:

{% extends blogPage|yesno:"blogPage.html,notBlogPage.html" %}

This solution will allow you to load a template that is dependent on the blogPage variable, effectively achieving the outcome of the if statement.

Upvotes: 2

Related Questions