I have base.html below:
# "templates/base.html"
{% block content %}{% endblock %}
And, I have index.html which extends base.html as shown below:
# "templates/index.html"
{% extends "base.html" %}
Then, I use {% if %} which is False outside of {% block %} in index.html as shown below but Hello World is displayed unexpectedly:
# "templates/index.html"
{% extends "base.html" %}
{% if False %}
{% block content %}
Hello World
{% endblock %}
{% endif %}
So, I use {% if %} inside of {% block %}, then Hello World is not displayed expectedly:
# "templates/index.html"
{% extends "base.html" %}
{% block content %}
{% if False %}
Hello World
{% endif %}
{% endblock %}
Also, I use {% with %} which defines name variable outside of {% block %} in index.html as shown below but John is not displayed unexpectedly:
# "templates/index.html"
{% extends "base.html" %}
{% with name="John" %}
{% block content %}
{{ name }}
{% endblock %}
{% endwith %}
So, I set {% with %} inside of {% block %}, then John is displayed expectedly:
# "templates/index.html"
{% extends "base.html" %}
{% block content %}
{% with name="John" %}
{{ name }}
{% endwith %}
{% endblock %}
Also, I use {% translate %} which defines title variable outside of {% block %} in index.html as shown below but This is the title. is not displayed unexpectedly:
# "templates/index.html"
{% extends "base.html" %}
{% load i18n %}
{% translate "This is the title." as title %}
{% block content %}
{{ title }}
{% endblock %}
So, I use {% translate %} inside of {% block %}, then This is the title. is displayed expectedly:
# "templates/index.html"
{% extends "base.html" %}
{% load i18n %}
{% block content %}
{% translate "This is the title." as title %}
{{ title }}
{% endblock %}
I know that the code outside of {% block %} doesn't affect the code inside of {% block %} because the code inside of {% block %} is evaluated first according to Template inheritance.
So, is it really impossible to expectedly run the code with {% if %}, {% with %} and {% translate %} outside of {% block %} in Django Templates?