在 Cakephp 中使用布局/元素
我正在使用 Cakephp 开发一个网络应用程序。我已经为网站创建了默认布局。我创建了页眉、页脚、内容等部分。
现在我的内容部分中的一些页面具有相同的结构。它们是控制器的一部分,为每个页面定义了各种视图。我应该在这里使用什么视图模板?布局还是元素?
如果我使用布局,我将使用控制器来决定我将使用的布局,但这不会改变整个网站的布局吗?
问候
I am developing a webapp using Cakephp. I have created a default layout for the website. I created sections like header, footer, content.
Now some of the pages in my content section have same structure. These are part of a controller with various views defined for each page. What view template should I be using here? Layouts or Elements?
In case I use layout, I will be using the controller to decide the layout I will use, but won't that change the layout for whole website?
Regards
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这确实是一个偏好问题。我尝试将布局视为页面的“结构”——即页眉、左侧导航占位符、顶部导航占位符、页脚。那种事。我在布局中定义这些部分。
我建议您将“内容部分中具有相同结构的页面”作为您的布局。正如您所提到的,您可以在控制器中切换布局,但任何类似的结构化页面(一旦删除内容,页面看起来彼此相同)我都会共享一个布局。
布局切换的另一个例子是登录用户与匿名用户。对于登录用户,网站经常在顶部放置“欢迎,$name”消息,以及指向“我的帐户”、注销等的链接。我经常发现自己对登录用户和匿名用户使用不同的布局。另外,在这种情况下,我使用 beforeFilter() 方法来设置给定控制器的布局,因为我知道执行回调时用户是否已登录。
很抱歉胡言乱语,但这有点灰色区域(何时使用布局与不同元素)。
This is really a matter of preference. I try to think of layout as the "structure" of the page -- so a header, placeholder for left nav, placeholder for top nav, footer. That kind of thing. I define those sections in the layout.
I'd recommend the "pages in my content section [that] have the same structure" be your layout. As you mentioned, you can switch layouts in the controller, but any similarly structured pages (pages that will look the same as each other once you've removed the content) I would have share a layout.
Another example of layout switching could be for logged in versus anonymous users. For a logged in user, websites frequently put a "Welcome, $name" message at the top, along with links to My Account, sign out, etc. I often find myself using a different layout for logged in versus anon users. ALso, in that situation, I use the beforeFilter() method to set the layout for a given controller, since I know whether the user is logged in or not when that callback is executed.
Sorry for the rambling, but this is a bit of a gray area (when to use a layout versus different elements).