How to set the content of the $script_for_layout in cakephp?
I am learning cakephp in an autodid开发者_运维问答act 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
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.
精彩评论