The newly updated version of CakePHP in version 1.3 packs a punch! It is now more easier to code as the Helper methods are better organized now.
For instance, linking external javascript in CakePHP 1.2 is quite an hassle as Javascript itself has Javascript Helper. However, The Javascript Helper is deprecated in 1.3 and will be removed in future versions of CakePHP. Henceforth, lets look at how HTML Helper load javascript.
In CakePHP 1.2, to load an external javascript, we do this:
<?php echo $javascript->link("jquery", true); ?>
Now in CakePHP 1.3, we can make use of HTML Helper, we do the following:
<?php echo $html->script("jquery"); ?>
Nevertheless, if you have multiple external js file to load, you can load it into an array:
<?php echo $html->script(array('jquery','wysiwyg','scripts')); ?>
Easy and more organized now eh? External CSS files were all along being load using HTML Helper. Now with the loading of Js files through HTML helper instead of invoking Javascript Helper, it is much more convenient! Moreover, you will only need to load one helper which is HTML Helper instead of loading 2.














12 Comments
hi willie,
Why when I use $html->script(“jquery”). I always get error “Method HtmlHelper::script does not exist” . Please help me if you can because I don’t know why it is.
Thanks,
Just a quick question, which cakephp version are you using now? It seem that the method script() cannot be found in HtmlHelper Class.
cakephp 1.3 Willie
It’s weird and I don’t know why
Hmm… can you download a fresh copy of CakePHP 1.3? Try overwriting the HtmlHelper Class with the fresh copy. It could solve this issue.
Nevertheless, can you also print-screen your page source where your $html->script() output the code? Send it to my mail. I will take a look and further advise you.
Hi Willie,
I replaced html.php and helper.php as your guide and I got message “Fatal error: Unsupported operand types in …\cake\libs\view\helpers\html.php on line 337″
I’m wrong when loving cakePHP
So is your problem solved ? heh
If you still got the message “Fatal error: Unsupported operand types in….”, my guess is that you are using CakePHP 1.2 instead of 1.3
Try download a fresh copy of CakePHP 1.3 and start baking!
This might be helpful
http://cakephp.1045679.n5.nabble.com/Unsupported-operand-types-td1314125.html
Accessing js files is very easy with cake1.3. However, I need the ability to insert javascript into the Head tag for a specific view. One way I found:
index.thtml: addHead .= ‘This will go in the head section’;?>
layout.thtml: addHead)) echo $this->addHead;?>
Is there a better way to selectively add javascript into particular view’s Head tags?
-Greg
Sorry.. not used to the blog stripping all the tags.. The second half would be to add statement in layout that says if isset to this->addHead.. then echo it- all placed in between the head tags of the layout.
I am wondering, why are you using .thtml for your view files. Shouldn’t it be .ctp since version 1.2?