2
我正在寻找一种干净的方式将变量传递给局部视图。请看下面的例子代码:将变量传递给局部视图的简洁方法
在我控制我做的:
$this->view->articles = $arrayWithArticles;
$this->render('articles.phtml');
在我articles.phtml观点我做的:
foreach($this->articles as $article) {
// show article
$this->render('comments.phtml');
}
在另一个控制器我做的:
$this->view->products = $arrayWithProducts;
$this->render('products.phtml');
在我的products.phtml观点我做的:
foreach($this->products as $product) {
// show product
$this->render('comments.phtml');
}
正如你可以看到我使用相同的(部分)视图comments.phtml显示约写的文章,以及产品的意见。我想要显示的'评论'在$article->comments
和$product->reviews
。部分视图将需要这些来显示它们。
什么是将它们传递给局部视图的干净方式。我真的不想做的事:
$this->comments = $article->comments;
$this->render('comments.phtml');
,因为这将有可能成为一个痛苦的跟踪关(即设置了同样的观点变量在两个控制器作为视图)。
是否有一个干净的解决方案将变量传递给部分视图?