CakePHP – link / include JavaScript files in layouts

JavaScript Libraries, OpenSource, PHP, Resources, Tips and Tricks, Tutorials
Create (or modify the) file [code lang="plain"]app/app_controller.php[/code] add this code [php] class AppController extends Controller { var $helpers = array('Html', 'Form', 'Javascript'); } [/php] CakePHP 1.2 allows you to define a reference to a Javascript file which is then added to the head section of the generated code. For this purpose the variable $scripts_for_layout has to be added to the layout: [php] <head> < ?php echo $scripts_for_layout; ?> </head> [/php] In the view you can now link to the Javascript file with: [php] $javascript->link('script.js', false); // OR you can simply use $javascript->link('script.js'); [/php]
Read More