CakePHP Quick Tip: $html->clearCrumbs

Published: on 16/10/08 | Comments (0)

Hi All

As part of an ongoing project, I was using the CakePHP's htmlHelper to generate breadcrumbs.

Now combined with the tree behavior's getpath function, the $html->addCrumb() and $html->getCrumbs() functions are great and a real time saver, but...

...for this particular page I actually want to show two or more different bread crumb trails, one for location, and one or more for categories, and that's where I hit a snag, as it stands the htmlhelper allows you to keep adding crumbs and displaying the trail so far, but has no way of clearing the protected _crumbs array, should you need to make more than one trail.

AppHelper to the rescue

Since the app_helper file was added to CakePHP you now have the facility to add additional functionality to built in helpers so by adding the following code to app_helper.php:

    /**
     * enhancement to htmlHelper which allows the crumbs protected array
     * to be cleared so that more than one set of crumbs can be generated in the same view.
     * 
     * @author Peter Butler peter@studiocanaria.com
     * @return void
     * @access public
     */
    function clearCrumbs(){
        $this->_crumbs = array();
    }

You can now use $html->clearCrumbs() to clear down and build a second, third or however many breadcrumb trails you need.

Hope you find this usefull,

till the next time.

Have your say:





Please enter the code