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.

You May Also Like…

ZPL SBPL

Zebra Printer Language http://labelary.com/viewer.html The above website has a page where you can enter ZPL and it...

PHP Iterators

Just came across a Youtube talk "Iterators in PHP" by Jake Smith published in 2014 that steps through the many...