<p>No super easy way around this one, I don't think. You might consider if using blocks is the most appropriate method in this case, or if a simple conditional in the extended template would do the trick:</p>

<p><strong>_layout.html</strong></p>

<pre><code>&lt;nav&gt;
    {% if category is not defined or category.title != 'Smith'%}
       {# Your navigation code here #}
        ...
    {% endif %}
&lt;/nav&gt;
</code></pre>

<p>If you want/need to use a block, I think your best option is to put the contents of the extended template's <code>nav</code> block (i.e. the default content for the nav block) in a partial template (e.g. <code>_partials/nav.html</code>), and include it like this in the layout and category templates, respectively:</p>

<p><strong>_layout.html</strong></p>

<pre><code>{% block nav %}
    {% include '_partials/nav' %}
{% endblock %}
</code></pre>

<p><strong>category.html</strong></p>

<pre><code>{% block nav %}
    {% if category.title != 'Smith' %}
        {% include '_partial/nav' %}
    {% endif %}
{% endblock %}
</code></pre>

<p>Yes, its one more template, but it's the most DRY solution I can think of.</p>

CC BY-SA 3.0
2015-10-20T18:15:18.347Z
2015-10-20T18:23:58.973Z
2015-10-20T18:23:58.973Z
Mats Mikkel Rummelhoff
<p>I apologize in advance if I am just overthinking this and missing something obvious (feels like I am). I have a situation on a category page where I am wanting to override a block in the document this one is extending, but only if the category is equal to a specific value ('Smith' in this case). If I put the conditional outside the block definition, I keep getting an internal server error. But if I put it inside the block definition, then it will override the block on every category page, not just this one. I'm trying to avoid having multiple templates and instead just hide one nav section on one category page. Please let me know if you need any more information. Thanks in advance for any advice!!</p>

<p>Here are the two versions of the code that I have tried. First gives the Internal Server error, second overrides the block for all pages.</p>

<pre><code>{% if category.title == 'Smith' %}
  {% block nav %} {% endblock %}
{% endif %}


{% block nav %}
  {% if category.title == 'Smith' %} {% endif %}
{% endblock %}
</code></pre>

<templating><conditional><category>
Conditional to override a block
# Conditional to override a block

(Asked by: username_0 on 2015-10-20)

I apologize in advance if I am just overthinking this and missing something obvious (feels like I am). I have a situation on a category page where I am wanting to override a block in the document this one is extending, but only if the category is equal to a specific value ('Smith' in this case). If I put the conditional outside the block definition, I keep getting an internal server error. But if I put it inside the block definition, then it will override the block on every category page, not just this one. I'm trying to avoid having multiple templates and instead just hide one nav section on one category page. Please let me know if you need any more information. Thanks in advance for any advice!!
Here are the two versions of the code that I have tried. First gives the Internal Server error, second overrides the block for all pages.
{% if category.title == 'Smith' %}
  {% block nav %} {% endblock %}
{% endif %}


{% block nav %}
  {% if category.title == 'Smith' %} {% endif %}
{% endblock %}

**Tags:** templating, conditional, category

---

## 1 answers

### Answer by username_1 on 2015-10-20. Score: 1

No super easy way around this one, I don't think. You might consider if using blocks is the most appropriate method in this case, or if a simple conditional in the extended template would do the trick:
_layout.html

    {% if category is not defined or category.title != 'Smith'%}
       {# Your navigation code here #}
        ...
    {% endif %}


If you want/need to use a block, I think your best option is to put the contents of the extended template's nav block (i.e. the default content for the nav block) in a partial template (e.g. _partials/nav.html), and include it like this in the layout and category templates, respectively:
_layout.html
{% block nav %}
    {% include '_partials/nav' %}
{% endblock %}

category.html
{% block nav %}
    {% if category.title != 'Smith' %}
        {% include '_partial/nav' %}
    {% endif %}
{% endblock %}

Yes, its one more template, but it's the most DRY solution I can think of.


---

