CakePHP breadcrumb with Ajax

Creating breadcrumb with cakephp is fairly simple task. One just need to put

<?php echo $this->Html->addCrumb('Users', '/users'); ?>

in view and

<?php echo $this->Html->getCrumbs(' >> ', 'Home'); ?>

it works good when site isn’t ajax but when one need to populate the web with ajax pages then throwing crumb gets clumsy.

One could output the crumbs in the view again, hidden, and then after .load() append them to the crumbs in your layout. You won’t be able to do it otherwise. The “layout” crumbs have already been rendered and won’t render again if you are using load() to inject content.

Some sample code to give you an idea of how I’d do it:

// layout
echo '<div class="crumbs">' . $this->Html->getCrumbs() . '</div>';
echo '<div class="content"><!-- AJAX content goes here--></div>';

// view
$this->Html->addCrumb('My Page');
echo '<div class="hiddenCrumbTrail">' . $this->Html->getCrumbs() . '</div>';
echo 'Here is my page content';

// js
$(function() { 
    $('.content').load('/controller/action'); // load content
    $('.crumbs').empty(); // remove existing
    $('.crumbs').append($('.hiddenCrumbTrail')); // append with updated
});