I would like to have a template that extends another conditionally. Basically, when a variable called "ajax" is true I DO NOT want to have the template extend another.
{% if not ajax %}
{% extends "/base.html" %}
{% endif %}
Any clues?
Join Stack Overflow to learn, share knowledge, and build your career.
I would like to have a template that extends another conditionally. Basically, when a variable called "ajax" is true I DO NOT want to have the template extend another.
{% if not ajax %}
{% extends "/base.html" %}
{% endif %}
Any clues?
You cannot do it like that. You can however set a variable and use that to choose the template to extend:
{% extends my_template %}
Then in python code you write something like:
if ajax:
template_values['my_template'] = 'base_ajax.html'
else:
template_values['my_template'] = 'base.html'
You may wish to refer to the documentation for more information.
While you may not wrap extends
in logic blocks, since it must be the first tag if used, it can still accept variables, including filters. This should fit your purpose nicely:
{% extends ajax|yesno:"base_ajax.html,base.html" %}
{# stuff #}
Note: the yesno
filter also accepts null values (None
) as the third choice, and if you do not specify one (as in this case), it will fallback to converting it to False
(i.e. it will return the second choice). This allows you to not specify the ajax
variable in your template without breaking it.
Suggested by user Rafael:
{% extends request.is_ajax|yesno:"base_ajax.html,base.html" %}
{# stuff #}
This will only work if you are using a RequestContext
context instead of a plain Context
object and you have the request
context processor enabled, or alternatively, if you insert the request object in your template context.
I was looking for the solution of the same problem and came with a bit better workaround than suggested by Klaus Byskov Hoffmann
. It is better because you don't have to have 2 separate base templates for ajax and non-ajax requests and, which is more important, you don't have to define if statement that will define which base template to use in EACH controller.
In your case the solution would be:
page.html
{% extends "/base.html" %}
{% block body %}
hello world
{% endblock body %}
base.html
{% if not ajax %}
<html>
<head></head>
<body>
LOGO and other stuff...
{% endif %}{% block body %}{% endblock body %}{% if not ajax %}
FOOTER
</body>
</html>
{% endif %}
So, base.html is always included but it prints its content only when not ajax
.
UPDATE: This can be simplified by creating and adding a new ConextProcessor that will populate ajax
context variable from the HttpRequest.is_ajax()
, so you don't have to do anything extra in your controllers and templates at all.
if
statement that will define the name of the base template in EACH controller. Also ajax
can be defined using context processor from HttpRequest.is_ajax()
. See updated answer.
– Vladimir Prudnikov
Feb 18 '14 at 8:17
If you don't want to "extend" a parent template, you can always create an empty file called null.html and extend that. Kind of hacky, but easy to understand.
{% extends "/base.html" %}
should be the first line, you can't add if statement.
– Vladimir Prudnikov
Jul 22 '11 at 11:10
I know this is an old thread but in case someone stumbles across this, I believe the best way to achieve your goal would be: "views.py"
if ajax:
request.session['ajax'] = 'ajax'
"base.html"
{% if 'ajax' in request.session %}
{% include "ajax.html" %}
{% endif %}
and in "ajax.html" you wouldn't use any {% extends %} or {% block %} tags since you won't be calling anything for "ajax.html" to extend onto the base. That will be taken care of by "include".