Test for existence of template block in a template

django, django-templates

Solution

You could double wrap it

{% block noheader %}
  <h2>{% block header %}Super Cool Page!{% endblock header %}</h2>
{% endblock noheader %}

On pages without header

{% block noheader %}{% endblock %}

Problem

I have a structure where there's normally a page heading in `(% block heading %}` in my base template: base.html ``` <h2>{% block heading %}{% endblock %}</h2> ``` Most of the time, I'll pass in a heading like this through templates that extend base: extends-base.html ``` {% block heading %}Super Cool Page!{% endblock %} ``` However, for a special page, I don't want to have a page heading: extends-base-special.html ``` {% block heading %}{% endblock %} ``` Ideally, this should exclude the `<h2>` tags. Now, I could just make the all of the extending templates include the `<h2>` tags, but that violates DRY, as every page should have the same element for the page-level heading. What I'd prefer to do is this (which doesn't appear to work): base-prefered.html ``` {% if heading %} <h2>{% block heading %}{% endblock %}</h2> {% endif %} ``` Is this doable somehow on the template level, or do I have to tuck into views for this?

Original source

Related problems