CakePHP - 包含基于特定页面的CSS?

发布于 2024-10-27 15:16:02 字数 264 浏览 2 评论 0原文

我想包含一个应应用于主页以及整个网站的 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 技术交流群。

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

发布评论

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

评论(4

似梦非梦 2024-11-03 15:16:02

我意识到我可以链接 CSS 文件
具体的观点,但是-然后他们
不会出现在 中。有没有一个
从视图链接并拥有它的方法
出现在头部吗?

是的,它们会在头脑中。请参阅 HTML 帮助程序文档

如果键“inline”设置为 false
$options 参数,链接标签是
添加到 $scripts_for_layout
您可以在里面打印的变量
文档的 head 标签。

因此,您视图中的这段代码...

$this->Html->css('my-css', null, array('inline' => false));

...会将正确的 元素添加到您的 中。

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?

Yes, they would be in the head. See the HTML Helper documentation:

If key 'inline' is set to false in
$options parameter, the link tags are
added to the $scripts_for_layout
variable which you can print inside
the head tag of the document.

So, this snippet in your view...

$this->Html->css('my-css', null, array('inline' => false));

...will add the proper <link> element to your <head>.

月下凄凉 2024-11-03 15:16:02

查看这个小教程:

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.

愁以何悠 2024-11-03 15:16:02

您还可以有不同的布局,并在其中包含 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.

真心难拥有 2024-11-03 15:16:02

这不是你问题的答案,但是......
您可以为布局文件编写自己的注入。 $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...

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