CakePHP Lightbox Helper

Made a helper to output lightbox compatible links, since the core HTML helper was not working. Stick the following code in app/view/helpers/lightbox.php:

class LightboxHelper extends AppHelper {

    var $helpers = array('Html');

    function img($thumb, $full, $imgAttributes = array(), $linkAttributes = array()) {
        $defaultLinkAttributes = array('rel'=>'lightbox');
        $linkAttributes = array_merge($defaultLinkAttributes, $linkAttributes);

        $thumb = $this->Html->image($thumb, $imgAttributes);
        if (strpos($full, '://') === false) {
            $full = $this->Html->webroot(IMAGES_URL . $full);
        }
        return $this->Html->output(sprintf($this->Html->tags['link'], $full, $this->Html->_parseAttributes($linkAttributes), $thumb));
    }
}

Then you can use it from your views, after including it in your controller’s $helpers array, like the following:

echo $lightbox->img('somethumb.jpg', 'somefullimage.jpg');

This entry was posted on Tuesday, July 22nd, 2008 at 1:24 pm and is filed under CakePHP. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

4 Responses to “CakePHP Lightbox Helper”

  1. elvy Says:

    Hi!
    I have my lightbox setup and I’m using your helper –
    I see all the js / css loading (through firebug) –
    but when I click on the image – I simply get forwarded to a page where only the image shows..(i.e. no pop-up/lightbox)

    Any clues what this has to do with?

    I use lightbox on non cakephp sites without a problem, and I see cakephp generating the same code – still not sure why it doesn’t work..

    thoughts?

  2. irina Says:

    I’ve tried your code but get a complain about imgController missing? Any suggestion why would that happen?

  3. irina Says:

    sorry for previous message – I’ve made a mistake deleting the folder with controllers that were required by other apps
    But my lightbox effect is not working even after following your suggested steps. Just opens the image in a parent window not even popup style :-( Any help?

  4. TI Fácil blog Says:

    Good morning, thanks for the interesting article? Is your blog a free theme or custom? I am interested by your site. Is it feasible to include this article on one of my sites?, i will of course link to this blog. regards

Leave a Reply