# \[solved\] Check if template exists before rendering

**URL:** https://discourse.slimframework.com/t/solved-check-if-template-exists-before-rendering/3169
**Category:** Questions
**Created:** [May 15, 2019, 11:05am UTC](https://discourse.slimframework.com/t/solved-check-if-template-exists-before-rendering/3169 "2019-05-15T11:05:22Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![xpico](https://avatars.discourse-cdn.com/v4/letter/x/9fc348/32.png) [@xpico](https://discourse.slimframework.com/u/xpico)
#### Post date: [May 15, 2019, 11:05am UTC](https://discourse.slimframework.com/t/solved-check-if-template-exists-before-rendering/3169/1 "2019-05-15T11:05:22Z")

</div>

Hi all,

I’m using slim with slim/twig-view. Anyone know if is there a way to check if template exists before calling to render?  
Something like:

```
If($this->view->exist('mytemplate.html')){
    return $this->view->render($response, 'mytemplate.html', $data);
}else{
    return $this->view->render($response, '404.html');
}

```

Thanks,

---

<div class="post-metadata">

### Author: ![Antnee](https://avatars.discourse-cdn.com/v4/letter/a/919ad9/32.png) [@Antnee](https://discourse.slimframework.com/u/Antnee)
#### Post date: [May 15, 2019, 11:31am UTC](https://discourse.slimframework.com/t/solved-check-if-template-exists-before-rendering/3169/2 "2019-05-15T11:31:56Z")

</div>

[https://www.php.net/manual/en/function.file-exists.php](https://www.php.net/manual/en/function.file-exists.php)

---

<div class="post-metadata">

### Author: ![\_i\_g](https://avatars.discourse-cdn.com/v4/letter/_/a587f6/32.png) [@\_i\_g](https://discourse.slimframework.com/u/_i_g)
#### Post date: [May 16, 2019, 2:43pm UTC](https://discourse.slimframework.com/t/solved-check-if-template-exists-before-rendering/3169/3 "2019-05-16T14:43:11Z")

</div>

You might have to do something like this:  
$view-\>getLoader()-\>exists(’@your/path)

I ended up customising slim/twig for this reason

---

<div class="post-metadata">

### Author: ![xpico](https://avatars.discourse-cdn.com/v4/letter/x/9fc348/32.png) [@xpico](https://discourse.slimframework.com/u/xpico)
#### Post date: [May 16, 2019, 3:20pm UTC](https://discourse.slimframework.com/t/solved-check-if-template-exists-before-rendering/3169/4 "2019-05-16T15:20:13Z")

</div>

ok, thank @_i_g , that works for me.

---

<div class="post-metadata">

### Author: ![Stjo](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/stjo/32/515_2.png) [@Stjo](https://discourse.slimframework.com/u/Stjo)
#### Post date: [May 20, 2019, 10:01am UTC](https://discourse.slimframework.com/t/solved-check-if-template-exists-before-rendering/3169/5 "2019-05-20T10:01:44Z")

</div>

You don’t mention what Slim version you are using? So I will assume its Slim3!?

If that’s the case, there’s actually a handler for this 🙂

See the official docs (they are pretty great at this)

[http://www.slimframework.com/docs/v3/handlers/not-found.html](http://www.slimframework.com/docs/v3/handlers/not-found.html)

---

<div class="post-metadata">

### Author: ![xpico](https://avatars.discourse-cdn.com/v4/letter/x/9fc348/32.png) [@xpico](https://discourse.slimframework.com/u/xpico)
#### Post date: [May 20, 2019, 6:56pm UTC](https://discourse.slimframework.com/t/solved-check-if-template-exists-before-rendering/3169/6 "2019-05-20T18:56:17Z")

</div>

Thank for the answer @Stjo, but that’s not my case. In my case i need to check if template exist inside one route. The route exist but i don’t know if template exist. It’s not usually case but…
