tags:

views:

158

answers:

1

Hi,

I am learning cakephp in an autodidact manner and I am a complete newbie :) I am creating a simple application. Some logic work so now I going to dive into designing views and layouts. I read the docs and the tutorial but I could not find where to set the content of $script_for_layout. Especially I want to set a $html->css to create link in pages to the stylesheet. I found out that I could do it in directly in the layout template but I would like to create the same link in all pages/views/layouts to the stylesheet so I hope there is a simple way, and avoid to do this in all layouts and/or controllers

A: 

You can do this with the JavascriptHelper. Load the helper via the controller's $helpers array.

# In your controller
class Things extends AppController {
  $helpers = array( 'Javascript' );

  # ... your custom controller code ... 

  # OR
  public method controllerAction( ... ) {
    $helpers[] = 'Javascript';

    # ... additional action code ...
  }
}

# In your view
$javascript->link( 'my_script', false );

In the view code, the false parameter adds /js/my_script.js to the set of scripts loaded by the $scripts_for_layout variable. To do the same thing for CSS, the key is the same $inline parameter. Set that value to false and the CSS file will load in the head as well:

$Html->css( 'view_css', 'stylesheet', array( "media" => "all" ), false );

Take a look at the documentation for general helper info and for the Javascript helper specifically.

Rob Wilkerson
Thanks, much clear now. What means if I do not add the false parameter? Does it mean that the script or css link will be inserted where it is in the view code? (I guess so but I did not found directly in the docs)
sipiatti
That's correct. It will add the script "inline". In other words, right where you have it in the view.
Rob Wilkerson