[solved] Check if template exists before rendering

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,

https://www.php.net/manual/en/function.file-exists.php

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

I ended up customising slim/twig for this reason

1 Like

ok, thank @_i_g , that works for me.

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 :slight_smile:

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

http://www.slimframework.com/docs/v3/handlers/not-found.html

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…