我正在用CakePHP编写一个博客,以便让自己开始使用这个框架。问题是,我已经得到了我的网站的布局,我想在其中显示最新的帖子(只要它不是索引页面)和标签云在所有情况下。问题是,我不能让每个控制器都给我的布局传递一个变量,这样它就可以渲染它,所以我需要一些更好的方法来查询我的帖子模型和标签云的标签模型,这是最好的方法吗?(以避免在视图中混合过多的逻辑)。
我的视图代码的相关部分如下:
<?php
if ($this->params['controller'] != 'posts' || $this->params['action'] != 'index') {
?>
<section class="box">
<header>
<h3>Latest posts</h3>
</header>
</section>
<?php
// display an unordered list with all the latest posts
}
?>
<section class="box">
<header>
<h3>Tag cloud</h3>
</header>
<!-- display another unordered list with the tags-->
</section>顺便说一下,我使用的是CakePHP 2.0.3
发布于 2011-11-24 17:06:59
您可以使用不同的布局。
主页布局-不显示最新帖子
/app/View/Layout/index.ctp Default layout -显示最新帖子和标签云
/app/View/Layout/default.ctp默认情况下,Cake将使用default.ctp布局,但您可以在控制器中覆盖该行为:
public function index() {
// do index stuff
$this->layout = "index";
}我还建议将你的最新帖子和标签云捆绑到元素中,这样可以更容易地将它们包含在布局中。
https://stackoverflow.com/questions/8251434
复制相似问题