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…

Toggen builds a new website

So I have built and hosted a new website for Roger Clark Pest Management they are a family owned and operated Pest...

WizTree

Just downloaded WizTree (not a sponsor) from https://wiztreefree.com/ It seems to do what it says on the tin which is...

Scam call from “Telstra”

Just had a call from a man with a sub-continental accent from caller ID 0734946596 Told me there was problem with my...