is there a simple way to exclude an 'include' for specific pages?

For instance I have an include {% include "_globals/_footer" %} but I don't want it to appear on my contact and 404 pages. How would I exclude it?

Thank you!


There may be a better way, including adding exclusions as an array and checking if the segment exists in the array, but what about wrapping it in an if tag? This seems to work.

{% if craft.request.firstSegment != 'contact' and craft.request.firstSegment != '404'  %}
    {% include 'layouts/partials/my-template'%}
{% endif %}

Quick update: This is very rough but an example with exclusions array (doesnt account for other segments, just an example...

{% set exclusions = ['contact','404'] %}
{% if craft.request.firstSegment not in exclusions  %}
    {% include 'layouts/partials/my-template'%}
{% endif %}
  • Thank you SO much! Never new about exclusions before this! – Spheriri Mar 31 '17 at 19:54
  • It's just a twig array (you can name it anything) then the if tag just makes sure the first segment (in my example) doesn't match anything in that array. Good luck. – Shift2Design Mar 31 '17 at 20:06

Another way would be to use:

{% include [
    '_globals/footer-' ~ craft.request.lastSegment,
] %}

You'd then have your regular footer.twig for all pages, but you could have a footer-404.twig that would just render on the 404 page.

Note: I think the previous solutions are better, but variety of solutions is always good, right?


Just in case anyone stumbles across this and is using craft 3 "craft.request.firstSegment" is now deprecated in favour of "craft.app.request.segments|first". So this is now:

{# Includes the footer on all pages except homepage, contact and 404 #}
{% set exclusions = ['', 'contact', '404'] %}
{% if craft.app.request.segments|first not in exclusions  %}
    {% include '_footer'%}
{% endif %}

You can use this answer to create Template Variable. Then use it like this:

{%- if craft.myPlugin.getHttpStatus() != 404 -%}
    {%- include "_globals/_footer" -%}
{%- endif -%}

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.