Cakephp:检查模板文件是否存在? [英] Cakephp : check template file exist or not?

查看:34
本文介绍了Cakephp:检查模板文件是否存在?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在控制器的Cakephp版本2.4中,呈现视图之前需要检查视图文件是否存在?然后决定渲染.例如,我需要像下面这样的代码.

In Cakephp version 2.4 in controller before rendering view need to check view file exist or not ? then decide to render. for example i need some code like below.

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

当前我使用try/catch,但是我寻求最佳方法.我需要功能 template_exist ,请问有什么可以帮助的吗?

Currently i use try/catch but i seek for best way.I need function template_exist can any one help please ?

推荐答案

您可以通过创建新的View对象,然后使用

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

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

这样,Cake将检查所有常用位置的元素模板.

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

这篇关于Cakephp:检查模板文件是否存在?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆