# How to render custom template and return proper status code?

**URL:** <https://discourse.slimframework.com/t/how-to-render-custom-template-and-return-proper-status-code/2420>\
**Category:** Questions\
**Created:** [May 15, 2018, 8:01pm UTC](https://discourse.slimframework.com/t/how-to-render-custom-template-and-return-proper-status-code/2420 "2018-05-15T20:01:39Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![keb](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/keb/32/682_2.png) [@keb](https://discourse.slimframework.com/u/keb)\
**Post date:** [May 15, 2018, 8:01pm UTC](https://discourse.slimframework.com/t/how-to-render-custom-template-and-return-proper-status-code/2420/1 "2018-05-15T20:01:39Z")

</div>

Currently using [Plates](http://platesphp.com/) for my templates, and I’m trying to render a custom template for error pages.

I noticed that if I do this:  
`return $response->withStatus(404);`

My application properly returns a response with a 404 status code. However, if I do either of these:  
`return $response->withStatus(404)->getBody()->write($template);`  
`return $response->withStatus(404)->getBody()->write('testing');`

My application incorrectly returns a response with a 200 status code:  
 ![firefox_2018-05-15_15-58-34](https://canada1.discourse-cdn.com/flex030/uploads/slimframework/original/1X/48f8abf9651d3283cf6be9762010c680210b880d.png)

Thanks in advance. I am doing this via a controller method whose parameters are:

```
Psr\Http\Message\ServerRequestInterface $req, Psr\Http\Message\ResponseInterface $res, array $args
```

---

<div class="post-metadata">

**Author:** ![tflight](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/tflight/32/1205_2.png) [@tflight](https://discourse.slimframework.com/u/tflight)\
**Post date:** [May 16, 2018, 11:35am UTC](https://discourse.slimframework.com/t/how-to-render-custom-template-and-return-proper-status-code/2420/2 "2018-05-16T11:35:22Z")

</div>

I haven’t used Plates, so I’m not certain what might be happening there. But this app works fine in my environment and returns a 200 response. Can you reduce your example down to something simple like this?

```php
<?php

require __DIR__. '/../vendor/autoload.php';

// Create and configure Slim app
$config = ['settings' => [
    'addContentLengthHeader' => false,
]];
$app = new \Slim\App($config);

// Define app routes
$app->get('/', function ($request, $response, $args) {
    return $response->withStatus(404)->getBody()->write('testing');
});

// Run app
$app->run();

```

---

<div class="post-metadata">

**Author:** ![keb](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/keb/32/682_2.png) [@keb](https://discourse.slimframework.com/u/keb)\
**Post date:** [May 16, 2018, 6:47pm UTC](https://discourse.slimframework.com/t/how-to-render-custom-template-and-return-proper-status-code/2420/3 "2018-05-16T18:47:57Z")

</div>

You may have misread, and I might be misunderstanding the functionality of `withStatus`. Do you mean your example returns a 404 response and not a 200 response as you mentioned?

If you do `return $response->withStatus(404);` I would assume this returns a 404 response.

Either way, I believe I have solved my problem. This seems to have worked:  
(Where `$this->templates` is an instance of `League\Plates\Engine`)

```auto
$template = $this->templates->render('TemplateFile', ['param' => $param]);
return $response->withStatus(404)->write($template);

```

I was under the assumption that you must `->getBody()` before you can `->write()` to the response, but this doesn’t seem to be the case. I’m not sure what the difference is, but using `->getBody()` beforehand was not giving the proper response code. If anyone can shed light on this, that would be great.

---

<div class="post-metadata">

**Author:** ![tflight](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/tflight/32/1205_2.png) [@tflight](https://discourse.slimframework.com/u/tflight)\
**Post date:** [May 16, 2018, 7:25pm UTC](https://discourse.slimframework.com/t/how-to-render-custom-template-and-return-proper-status-code/2420/4 "2018-05-16T19:25:54Z")

</div>

Right, sorry, I misread. 😳

If you have a look at the `write` method on Response, it looks like it is a shortcut to getting the response’s body stream and writing to it, performing `getBody()` itself.

> <https://github.com/slimphp/Slim/blob/0803c9ed901db504a42b1f163850b6d5c7c4694a/Slim/Http/Response.php#L300>
