<p>No, the <code>craft.request</code> (<a href="https://craftcms.com/classreference/variables/HttpRequestVariable" rel="nofollow"><code>HttpRequestVariable</code></a>) class doesn't expose the HTTP status code.</p>

<p>It'd be super easy to add it to Twig as <a href="https://craftcms.com/docs/plugins/variables" rel="nofollow">a variable method in a custom plugin</a>, though:</p>

<pre><code>namespace Craft;

class MyPluginVariable {

    public function getHttpStatus()
    {
        return http_response_code(); 
    }

}
</code></pre>

<p>...and in your template:</p>

<pre><code>{{ craft.myPlugin.getHttpStatus() }}
</code></pre>

CC BY-SA 3.0
2016-02-01T14:37:50.990Z
Nice one! thanks for the clarification and tip :)
Alec Ritson
CC BY-SA 3.0
2016-02-01T18:29:26.817Z
Good answer though not sure why this is actually necessary in practice? If the request gets to Twig, it's either going to be 200 or an error that Craft intercepts and passes along (404, 500, 403) in which case you could just load the appropriate template... eg 404.html, 503.html, etc
RitterKnight
CC BY-SA 3.0
2016-02-01T18:30:33.230Z
@RitterKnight I concur, also curious about the use case here :)
Mats Mikkel Rummelhoff
CC BY-SA 3.0
2016-02-26T07:02:59.797Z
The use case was to build a plugin which detects the status code and if it's a 500 or 404 or whatever would ping an email to the system admin with details so you could display a "something went wrong but we have been notified" kinda thing :)
Alec Ritson
CC BY-SA 3.0
2016-02-01T13:08:20.820Z
2016-02-01T13:08:20.820Z
Mats Mikkel Rummelhoff
<p>Since this is the top hit in Google, I'll guess it's handy to mention that <code>craft.app.response.statusCode</code> will display the statuscode in Craft v3 and up.</p>

CC BY-SA 4.0
2020-01-13T10:17:17.480Z
2020-01-13T10:17:17.480Z
NxWebster
<p>I need a nice way to determine whether I'm on a <code>200</code> page. Basically anything other than a status page (404, 500, 403) in twig templates.</p>

<p>I thought I could do something like <code>craft.request.getStatus</code> or <code>craft.request.status</code> but this didn't work.</p>

<p>From looking in the <code>HttpRequestService.php</code> file there doesn't seem to be anything to retrieve this info so I wondered if anyone has achieved something similar.</p>

<templating><request>
Can you get the HTTP status code in twig templates?
# Can you get the HTTP status code in twig templates?

(Asked by: username_0 on 2016-02-01)

I need a nice way to determine whether I'm on a `200` page. Basically anything other than a status page (404, 500, 403) in twig templates.

I thought I could do something like `craft.request.getStatus` or `craft.request.status` but this didn't work.

From looking in the `HttpRequestService.php` file there doesn't seem to be anything to retrieve this info so I wondered if anyone has achieved something similar.

**Tags:** templating, request


---

## 2 answers

### Answer by username_1 on 2016-02-01. Score: 3 | Accepted answer

No, the craft.request (HttpRequestVariable) class doesn't expose the HTTP status code.
It'd be super easy to add it to Twig as a variable method in a custom plugin, though:
namespace Craft;

class MyPluginVariable {

    public function getHttpStatus()
    {
        return http_response_code(); 
    }

}

...and in your template:
{{ craft.myPlugin.getHttpStatus() }}

> **Comments:**
> **username_0**: Nice one! thanks for the clarification and tip :)  
> **username_2**: Good answer though not sure why this is actually necessary in practice? If the request gets to Twig, it's either going to be 200 or an error that Craft intercepts and passes along (404, 500, 403) in which case you could just load the appropriate template... eg 404.html, 503.html, etc  
> **username_1**: @username_2 I concur, also curious about the use case here :)  
> **username_0**: The use case was to build a plugin which detects the status code and if it's a 500 or 404 or whatever would ping an email to the system admin with details so you could display a "something went wrong but we have been notified" kinda thing :)  

---

### Answer by username_3 on 2020-01-13. Score: 9

Since this is the top hit in Google, I'll guess it's handy to mention that `craft.app.response.statusCode` will display the statuscode in Craft v3 and up.


---

