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!
Craft CMS Stack Exchange is a question and answer site for administrators, end users, developers and designers for Craft CMS. It only takes a minute to sign up.
Sign up to join this communityis 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 %}
Another way would be to use:
{% include [
'_globals/footer-' ~ craft.request.lastSegment,
'_globals/footer'
] %}
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?
You can use this answer to create Template Variable. Then use it like this:
{%- if craft.myPlugin.getHttpStatus() != 404 -%}
{%- include "_globals/_footer" -%}
{%- endif -%}
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 %}