Gallery Application using Cakephp and Jquery

Recently I’m working with CakePHP and SQLite to develop a gallery for a client. This is going to be my first CakePHP app.
While I’m working on the admin section, I wanted to allow the client to see the image once the image filename field is out of focused so that he could see if it’s the correct image.
The only JS library included with CakePHP is Prototype. Fortunately, the latest version of CakePHP (1.3.11 as of right now) allows us to include with other JS library. I included jQuery with the help of Js helper:
?
1
var $helpers = array("Js"=>array("Jquery"));
The above code is included with the whatever controller(s) you want to use this helper. In my case is galleries_controller.php and images_controller.php.
Then I just need to add the following code into the corresponding views, in my case is some of the admin views (admin_edit.ctp, and admin_add.ctp):
1
2
3
echo $this->Js->set("imageLoc", $this->Html->image("0"));
echo $this->Js->get("#GalleryCover")->event("blur", "$('#cover').html(window.beta.imageLoc.substring(0, window.beta.imageLoc.length-12) + 'images/' + $('#GalleryPath').val() + '/thumb/' + $(this).val() + '\" />');");
Note that I used image object in the Html helper to generate a dummy img tag so that I could precisely get the image folder location in the webroot folder. Then I used set object in the Js helper to transfer this generated PHP value to Javascript variable “imageLoc”. The second line is self-explanatory if you know jQuery. Here is the documentation in the CakePHP Cookbook for you to review.
In the jQuery code section of the second line, I used the substring function to remove the last 12 characters in the img tag (0″ alt=”" />). And used the html function to include the generated code into the “cover” div. Every time the “GalleryCover” field is blurred out of focus, the image inside the “cover” div would change to the corresponding images.
If you need more information on how to use CakePHP generated Javascript variables, please refer to the second article in the reference section.
Do you have any other tips on how to incorporate jQuery into CakePHP? Please share them in the comment below.
Reference

This entry was posted in . Bookmark the permalink.

Leave a Reply