1

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 %}
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

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

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.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.