CakePHP Javascript Include

Written by James McDonald

October 6, 2011

I read the documentation. But I must be a slow learner or something.

Anyway including javascript into a view using cakephp:

I am using an auto baked cake template named index.ctp

For me it’s located cake_app_dir/views/names/index.ctp

To get the javascript script block into your page I needed to add the following in the index.ctp page:

< ?php $this->Html->script(array ( 'jquery', 'script2', 'script3'), false);?>

Where the array contains the name of the javascript file sans the dot js extension of each file on you want to include

You can create custom locations for javascript under webroot e.g.

mkdir -p cake_app_dir/webroot/test/one/two

Put your javascript into the custom dir and specify the path as follows

< ?php $this->Html->script(array ( 'jquery', 'script2', 'script3', '/test/one/two/script4.js' ), false);?>

specifying false tells it to output the block where the $scripts_for_layout echo statement is in cake_app_dir/views/layouts/default.ctp

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

You May Also Like…

How to Research a CPU Upgrade

How to Research a CPU Upgrade

Upgrade Time! Doing a lot of VMWare Workstation virtualization to create labs for self-study and training. Finding...