4

Attempting to include a template that doesn't exist throws HttpStatus 500 – Craft\TemplateLoaderException – Unable to find the template “{template}”.

This is usually fine and what you want, but is it possible to override this behaviour somehow, and make Craft throw a plain' old 404 instead?

Mats Mikkel Rummelhoff
  • 22,361
  • 3
  • 38
  • 69

1 Answers1

6

Here's one way to do it – not super elegant, but it works:

{% set template %}
    {{- include('sometemplatethatdoesntexist', ignore_missing = true) -}}
{% endset %}

{% if not template %}
    {% exit 404 %}
{% endif %}

{{ template }}
Mats Mikkel Rummelhoff
  • 22,361
  • 3
  • 38
  • 69