7

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.

1
  • thanks for the edit - I guess it hid my head tag - i'm a stockoverflow noob :) Commented Mar 29, 2011 at 14:17

4 Answers 4

19

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>.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks a lot for leading me in the right direction! To clarify your answer for anyone else who is confused like me, you can "include" your css file from your view, but doing so via Cake's Html->css code, and setting it's option to inline=>false, like mentioned in above answer, it will add the css IN the head tag like you want it. It didn't work for me until I added null as the 2nd option though: <?php echo $this->Html->css('layout1', null, array('inline'=>false)); ?> Not sure if I did something wrong, or if you have to include the 2nd paramater as mentioned in the documentation.
@Dave: You are correct. The options array is the third argumentm not the second argument. I updated my answer.
$this->Html->css('my-css', [], array('inline' => false)); since cakephp 4
0

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.

Comments

0

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.

Comments

0

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...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.