CSS files and $scripts_for_layout

You probably know that if you have $scripts_for_layout in your site’s layout, then you can include JS files from the view. For example, if you’d like to include ‘myScript.js’ from some view you would add the following code: $javascript->link(‘myScript’, false);

Basically the second param (false), will tell CakePHP not to include the file in-line, but rather where the $scripts_for_layout is located.

A less known feature is that you can do the same for your CSS files. So, if you need to include some style that is specific to a given view, add something like this to your view file:

$html->css(‘myViewCss’, ‘stylesheet’, array(“media”=>”all” ), false);

Again, you simply need to specify a ‘false’ parameter. This will tell CakePHP to include the CSS file where the $scripts_for_layout variable is located (which should be in the ).

m4s0n501
  • http://snowdog.pl snowdog

    Thanks, I was looking for this for a long time and couldn’t find a solution (tried my own implementation of css_for_layout etc.)

  • teknoid

    @snowdog

    Glad it helped ;)

  • Leonardo

    I don’t know what I am missing here.

    I can’t include the CSS files in the head.

    Look my code:

    charset(); ?>

    css(‘estilos.css’, ‘stylesheet’, array(), false);
    echo $scripts_for_layout;
    ?>

  • Leonardo
  • teknoid

    @Leonardo

    This is best used if you are including CSS from the view. Your example shows that you are doing it in the layout, but there is no point in doing that, since you have full control of where the CSS appears.

  • Leonardo

    Yeah, I know.

    My javascripts add like this in another page:

    link(array(‘jquery’, ‘jquery.maskedinput’, ‘jquery.cssCheckbox’), false); ?>

    Didnt work too.

    If I added this in the head of my default.ctp:

    Mysteriously appears in the body section.

    I dont know what’s happening. :(

  • Leonardo

    U can erase my last comment:
    Here a example:
    http://rafb.net/p/5dO3XO75.html => http://rafb.net/p/KZxsxN39.html

  • teknoid

    @Leonardo

    What version of cake are you using?
    You might want to ask this at the google group or IRC to get better/more answers.

  • Leonardo

    Finally…
    The problem is the encode. Change to UTF-8 without BOM and its working now :)
    Some problem with the editor, i dont know.

  • teknoid

    @Leonardo

    Whoa, that’s a tricky one. Good to hear you got is resolved.

  • Pingback: [PRONIQUE] CakePHP Developer Links()

  • Pingback: CSS files and $scripts_for_layout | Black Sheep Designz()

  • http://sembang.my Halid Juki

    Thanks for the clear explanations… appreciate it..

  • http://teknoid.wordpress.com teknoid

    @Halid Juki

    No problem ;)

  • http://www.btodoli.net Bernardino Todolí

    Great help!
    Thanks

  • Carlos

    Fantastic tip. Extremely helpful. Thanks for sharing!

  • http://teknoid.wordpress.com teknoid

    @Bernardino Todolí and Carlos

    Thanks. Glad to help.

  • http://www.ilexius.de ilexius it software

    Which version of Cake are you using? I can’t get it to work with 1.3 so I am using a workaround atm:

    $this->addScript($html->css('modalbox', null, array("media"=>"screen")));

    I think the problem is caused by the code in cake/libs/view/helpers/html.php:

    336    function css($path, $rel = null, $options = array()) {
    337   $options += array('inline' => true);

    Doesn’t this overwrite inline to true?

  • http://teknoid.wordpress.com teknoid

    @ilexius it software

    This was written during the days of 1.2

    In 1.3 the syntax is:
    $this->Html->css(‘style.css’, NULL, array(‘inline’ => FALSE));

  • jonh

    and how to code in cakephp version 2.x?

  • teknoid

    @jonh

    same. the syntax is a little different, since the writing… check the docs.

  • Jens Stigaard

    Syntax in 2.x as following:

    $this->Html->css(‘my-css’, null, array(‘inline’ => false));
    found at:http://stackoverflow.com/questions/5464894/cakephp-include-css-based-on-specific-pages