0

我想渲染其中一组视图:

  1. 身体-$id1
  2. 脚丫子

或者

  1. 身体-$id2
  2. 脚丫子

哪个集合存在。

我这样做:

try {
    $this->render("head");
    $this->render("body-$id1");
    $this->render("foot");
} catch (Exception $e) {
    $this->render("head");
    $this->render("body-$id2");
    $this->render("foot");  
}

但是head如果 body-$id1 不存在,它会导致视图被渲染两次。

你有更好的解决方案吗?

换句话说,我可以body-$id1在渲染之前检查它的存在吗?

4

1 回答 1

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}”。

于 2010-03-13T18:10:11.540 回答