Vishal
Vishal

Reputation: 2336

Django template blocks

I currently have...

<title>
MyApp | {% block customtitle %}{% endblock %}
</title>

What I want really is for the | to only appear if the customtitle block is NOT empty. I only want to see MyApp in the title if the page does not set the customtitle block.

Is that an option? How would one implement it?

Upvotes: 2

Views: 349

Answers (1)

e4c5
e4c5

Reputation: 53734

Much simpler to define your base template like this:

<title>
{% block customtitle %}MyApp {% endblock %}
</title>

then in your derived templates

{% block customtitle %}
{{ block.super }} | Custom Title
{% endblock %}

The presence of block.super will ensure that MyApp will appear in all the pages. Using block.super instead of hard coding MyApp will ensure that changing the base template in the future will not break the title.

Upvotes: 5

Related Questions