CakePHP Javascript Include

I read the documentation. But…

Login

Blog History

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 *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

The reCAPTCHA verification period has expired. Please reload the page.