Take the 2-minute tour ×

I have a quick question here. Let say I have a view file myView.ctp in cakePHP and inside my view I have some javascript (which I have there for a reason). I know I can tell cake to put my javascript code into the header section of my page by using the scriptStart() and scriptEnd() blocks like:

<?php $html->scriptStart(array('inline' => false)); ?>

// My script code goes here...

<?php $html->scriptEnd(); ?>

The array('inline' => false) is what actually tells cake to put my script in the header. Now my question is this: How do I achieve the same thing for css codes (WITHOUT putting my css codes into an external file)? This techniques seem to only work for javascript codes.

Thank you

share|improve this question
Styles always belong into the head. Maybe you can try style() and if not fitting search the docs, e.g. css() then hacking cakePHP to add the functionality you need. –  hakre Mar 15 '12 at 15:23
add comment

2 Answers

i think you can use this link api13.cakephp.org/class/html-helper

and view style topic. for view example use this link

share|improve this answer
add comment
$css = $this->Html->tag('style', '/* my css */');

The addScript() function on the view will append your script to the $scripts_for_layout var.

Edit: Comment reiterated something important I missed so I revised the answer.

share|improve this answer
"WITHOUT putting my css codes into an external file" –  Dave Mar 15 '12 at 18:50
@Dave Whoops totally missed that. Revised the answer. –  jeremyharris Mar 20 '12 at 13:45
Still missed the point - he wants to be able to write the style code in his view, but have it populate in his header (layout) file. –  Dave Mar 20 '12 at 15:39
Wow I'm terrible at this, huh? Giving it another go... –  jeremyharris Mar 20 '12 at 17:08
Not able to test, but removed my downvote now, as it appears your answer is on track :) –  Dave Mar 20 '12 at 18:46
show 1 more comment

Your Answer


