CakePHP: check template file exists or not?

In Cakephp version 2.4 in the controller, before rendering, do I need to check the view file or not? then decide to do. For example, I need code as shown below.

if( template_exist( $template_name ) ) { $this->render( $template_name ); } else { $this->render( $default_template ); } 

I am currently using try / catch, but I am looking for a better way. I need a template_exist function. Can anyone help?

+1
source share
1 answer

You can do this with the controller by creating a new View object and then elementExists() to check if the template exists: -

 $View = new View($this, false); if ($View->elementExists($templateName) === true) { $this->render($templateName); } else { $this->render($defaultTemplate); } 

This way Cake will check all the usual places for element templates.

+1
source

All Articles