Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django templates syntax error

Is there any problem with the syntax in the following code, there is a error as Invalid block tag: 'else'

{% ifequal chat_profile 1 %}
    {% extends "chatprofile/chat_profile1.html" %}
{% else %}
    {% extends "chatprofile/chat_profile.html" %}
{% endifequal %}
like image 790
Rajeev Avatar asked Dec 28 '22 05:12

Rajeev


2 Answers

True, you must use extends as the first tag, but you can also pass it a variable instead of a fixed string:

{% extends base %}

Then you can include a context variable named base with the name of the template to inherit from, e.g.:

    return render_to_response('my_template.html',
                          { 'base': 'chatprofile/chat_profile1.html' })
like image 75
Carles Barrobés Avatar answered Jan 04 '23 14:01

Carles Barrobés


The documentation states:

If you use {% extends %} in a template, it must be the first template tag in that template. Template inheritance won't work, otherwise.

So consider using a design where you can use {% include %} instead.

like image 39
Klaus Byskov Pedersen Avatar answered Jan 04 '23 13:01

Klaus Byskov Pedersen