我想渲染其中一组视图:
OR
设置了exsists。
我是这样做的:try {
$this->render("head");
$this->render("body-$id1");
$this->render("foot");
} catch (Exception $e) {
$this->render("head");
$this->render("body-$id2");
$this->render("foot");
}
但如果body- $ id1不存在,则会导致head
视图呈现两次。
你有更好的解决方案吗?
在另一个说法中,我可以在呈现之前检查body-$id1
的存在吗?
答案 0 :(得分:1)
好吧,它会在“try”块中运行任何有效的脚本,但如果失败,它将呈现“catch”块中的所有内容。所以你可能想要更像的东西:
$this->render("head");
try {
$this->render("body-$id1");
} catch (Exception $e) {
$this->render("body-$id2");
}
$this->render("foot");
我没有看到用于检查视图是否存在的API方法,但是您可以编写一个控制器助手,它只获取视图脚本的路径并使用file_exists
检查“body - {$ id1 “存在于那条道路上。