<p>If you don't provide a status code in your exit tag, e.g.</p>
<pre><code>{% exit 401 %}
</code></pre>
<p>Then no exception is thrown, it just ends the application with</p>
<pre class="lang-php prettyprint-override"><code>Craft::$app-&gt;end();
</code></pre>
<p>This will terminate the application immediately, and any output buffer (meaning your Twig code in this case) will never be flushed and sent to the browser.</p>
<p>This is intentional by design. The reason why it works in local dev, is that Yii2 will throw an <code>ExitException</code> if it is in <code>YII_ENV_TEST</code>, which is set by the Craft setting <code>devmode</code>:</p>
<pre class="lang-php prettyprint-override"><code>        if (YII_ENV_TEST) {
            throw new ExitException($status);
        }
</code></pre>
<p>A thrown exception will flush the output buffer, thus it &quot;worked&quot; in local dev.</p>
<p>I'm thinking a solution here would be to use <code>{% redirect %}</code> instead, to redirect to a friendly error page of some kind?</p>
<p>ref: <a href="https://craftcms.com/docs/3.x/dev/tags.html#exit" rel="nofollow noreferrer">https://craftcms.com/docs/3.x/dev/tags.html#exit</a></p>

CC BY-SA 4.0
2022-08-16T13:26:40.780Z
Thank you for the explanation as to why this "worked" on my local - it's much appreciated. I'm in an odd situation but rather than displaying an error message, I'm trying to render a value from the `entry` object on the basis of a querystring which will be collected via AJAX, and the page exit is to avoid unnecessary further loading. I might be barking up the wrong tree and will rethink a solution. Thanks again!
Chris
CC BY-SA 4.0
2022-08-17T02:51:17.790Z
In that case, I would just use a conditional. It will render the entire page anyway, there is no need to exit
andrew.welch
CC BY-SA 4.0
2022-08-17T08:44:04.733Z
That's good to know, thanks!
Chris
CC BY-SA 4.0
2022-08-16T12:49:23.177Z
2022-08-16T12:49:23.177Z
andrew.welch
<p>I'm trying to get my site to display a message and exit on the basis of a querystring in the URL. I have this code at the top of my main template:</p>
<pre><code>{% set queryString = craft.app.request.queryString %}

{% if 'testExit=1' in queryString %}
    Test Exit.
    {% exit %}
{% endif %}
</code></pre>
<p>This works as expected on my local environment (I see the message &quot;Test Exit&quot;), but when I try on the production website, the page is blank.</p>
<p>Is there a way I can fix this, a config setting to tweak perhaps?</p>
<p>I'm running Craft CMS 3.7.45.1.</p>

<craft3><templating>
Behaviour around {% exit %} is different on local VS production
# Behaviour around {% exit %} is different on local VS production

(Asked by: username_0 on 2022-08-16)

I'm trying to get my site to display a message and exit on the basis of a querystring in the URL. I have this code at the top of my main template:
{% set queryString = craft.app.request.queryString %}

{% if 'testExit=1' in queryString %}
    Test Exit.
    {% exit %}
{% endif %}

This works as expected on my local environment (I see the message "Test Exit"), but when I try on the production website, the page is blank.
Is there a way I can fix this, a config setting to tweak perhaps?
I'm running Craft CMS 3.7.45.1.

**Tags:** craft3, templating


---

## 1 answers

### Answer by username_1 on 2022-08-16. Score: 2 | Accepted answer

If you don't provide a status code in your exit tag, e.g.
{% exit 401 %}

Then no exception is thrown, it just ends the application with
Craft::$app->end();

This will terminate the application immediately, and any output buffer (meaning your Twig code in this case) will never be flushed and sent to the browser.
This is intentional by design. The reason why it works in local dev, is that Yii2 will throw an ExitException if it is in YII_ENV_TEST, which is set by the Craft setting devmode:
        if (YII_ENV_TEST) {
            throw new ExitException($status);
        }

A thrown exception will flush the output buffer, thus it "worked" in local dev.
I'm thinking a solution here would be to use {% redirect %} instead, to redirect to a friendly error page of some kind?
ref: https://craftcms.com/docs/3.x/dev/tags.html#exit

> **Comments:**
> **username_0**: Thank you for the explanation as to why this "worked" on my local - it's much appreciated. I'm in an odd situation but rather than displaying an error message, I'm trying to render a value from the `entry` object on the basis of a querystring which will be collected via AJAX, and the page exit is to avoid unnecessary further loading. I might be barking up the wrong tree and will rethink a solution. Thanks again!  
> **username_1**: In that case, I would just use a conditional. It will render the entire page anyway, there is no need to exit  
> **username_0**: That's good to know, thanks!  

---

