[solved] Check if template exists before rendering

#1

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,

#2

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

#3

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

I ended up customising slim/twig for this reason

1 Like
#4

ok, thank @_i_g , that works for me.

#5

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

#6

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…