根据当前记录使用 CakePHP 自定义布局

发布于 2024-10-20 18:51:37 字数 295 浏览 3 评论 0原文

有谁对如何不基于当前视图而是基于与视图关联的数据自定义站点范围内的布局有建议吗?在大多数情况下,我们使用的模型都有一个关联的俱乐部 ID,因此必须根据当前页面与哪个俱乐部关联来自定义布局标题图像、CSS 等。

这是我到目前为止的想法。 调用与当前控制器关联的模型中的函数,该函数为您提供布局参数。 在appController中,beforeRender中,设置俱乐部的布局参数。

这看起来不太优雅,因为每个模型都必须有这个函数,如果 beforeRender 在应用程序控制器中定义,我将如何调用正确的模型?

一些提示会很棒!

Does anyone have recommendations on how to customize the layout sitewide based not on the current view, but the data associated with the view? In most cases, the models we are using have an associated Club id, so would have to customize the layout header image, css, etc, depending on which Club the current page is associated with.

Here is what I am thinking so far.
Call a function in the model associated with the current controller, which gives you the layout parameters.
In appController, beforeRender, set layout parameters for the club.

This doesn't seem very elegant, because each model would have to have this function, and how would I call the right model if beforeRender is defined in the app controller?

Some tips would be great!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

就是爱搞怪 2024-10-27 18:51:37

拉塞尔,

你可能想看看这个

Russel,

you might want to look at this.

挥剑断情 2024-10-27 18:51:37

只需在名为“dashboard.ctp”的布局中创建一个文件,然后如果您的视图是索引,您的函数将是这样的...

    function index() {
    $this->User->recursive = 2;
            $this->layout = 'dashboard';
    $this->set('users', $this->paginate());
}

重要的一行是 $this->layout = 'dashboard';

Simply create a file in layouts called "dashboard.ctp" then if your view is index, your function would be something like this...

    function index() {
    $this->User->recursive = 2;
            $this->layout = 'dashboard';
    $this->set('users', $this->paginate());
}

The important line being $this->layout = 'dashboard';

伴我老 2024-10-27 18:51:37

我们最终要做的是在应用程序控制器中检查当前正在加载站点的域,然后根据此信息设置布局(为站点需要定制的每个域提供一个布局)。这不是一个很好的解决方案,但效果很好。

What we ended up doing is checking in the app controller for the domain the site is currently being loaded from, then setting the layout based on this information (have a layout for each domain the site needs to look customized for). Not a great solution but it works fine.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文