CakePHP - 包含基于特定页面的CSS?
我想包含一个应应用于主页以及整个网站的 6-7 个其他页面的特定 css 文件。
我知道我可以通过 PHP 做到这一点,获取 URL,找出哪个页面,链接 css...等等,但我想知道是否有一种灵活的方法(或任何更好的方法)使用 CakePHP 来包含正确的 css 文件(s)。
我意识到我可以从特定视图链接 CSS 文件,但是 - 那么它们就不会在 中。有没有办法从视图链接并将其显示在头部?
我希望我的问题有意义,并非常感谢任何帮助。
I want to include a specific css file that should be applied to the homepage, and 6-7 other pages throughout my site.
I know I can do this via PHP, getting the URL, finding out what page, linking the css...etc, but I was wondering if there was a slick way (or any better way) using CakePHP to include the correct css file(s).
I realize I can link the CSS file from the specific views, but - then they wouldn't be in the <head>
. Is there a way to link from a view and have it show up in the head?
I hope my questions make sense, and greatly appreciate any help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
是的,它们会在头脑中。请参阅 HTML 帮助程序文档:
因此,您视图中的这段代码...
...会将正确的
元素添加到您的
中。
Yes, they would be in the head. See the HTML Helper documentation:
So, this snippet in your view...
...will add the proper
<link>
element to your<head>
.查看这个小教程:
http: //nuts-and-bolts-of-cakephp.com/2008/05/05/css-files-and-scripts_for_layout/
基本上你可以使用这个标准视图属性 $scripts_for_layout 来基于视图注入 CSS 文件。希望这就是您正在寻找的。我想到了其他一些疯狂的选项,涉及扩展解析,但它可能比仅仅手动链接样式表更麻烦。我认为这个链接描述了最好的解决方案。
Check this little tutorial out:
http://nuts-and-bolts-of-cakephp.com/2008/05/05/css-files-and-scripts_for_layout/
Basically you can use this standard view attribute $scripts_for_layout to inject CSS files based on the view. Hope this is what you're looking for. There's a few other crazy options I thought of, involving extension parsing, but it would probably be more cumbersome than just manually linking the stylesheets. I think this link describes the best solution.
您还可以有不同的布局,并在其中包含 css:
http: //book.cakephp.org/view/1080/Layouts#!/view/1080/Layouts
如果模型的所有视图都具有相同的 css 或脚本,这会很方便。
You can also have different layouts, and include css in them:
http://book.cakephp.org/view/1080/Layouts#!/view/1080/Layouts
This comes handy if all the views of a model have the same css, or script.
这不是你问题的答案,但是......
您可以为布局文件编写自己的注入。 $scripts_for_layout 的问题在于 header 中注入了 js 和 css 代码。如果您编写自己的实现,则可以将 $scripts_for_layout var(对于 js)放在布局文件的末尾。暗示是:分离……
It is not the answer for your question, but...
you can program your own injection for the layout file. The problem with $scripts_for_layout is that js and css codes are injected in the header. If you write your own implementation you can put the $scripts_for_layout var (for the js) at the end of the layout file. The cue is: separation...