检查 cakephp 3 中是否存在一个元素,一个 .ctp 文件的最佳方法是什么?
像这样的东西,
<?php
// if the elemet exist display it
$this->render('/Element/Trip/'.$current_step);
// else display another element instead
?>
Run Code Online (Sandbox Code Playgroud)
谢谢 :)
使用$this->elementExists:
<?php
if ($this->elementExists('Trip/'.$current_step)) {
echo $this->element('Trip/'.$current_step);
} else {
echo $this->element('Trip/default.ctp');
}
Run Code Online (Sandbox Code Playgroud)
http://api.cakephp.org/3.2/class-Cake.View.View.html#_elementExists
如果您不想显示另一个元素但不抛出异常,则可以使用ignoreMissing选项
$this->element('Trip/'.$current_step,$data, ['ignoreMissing' => true])
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1535 次 |
| 最近记录: |