简体   繁体   中英

cakephp addCrumb Html Helper

I'm trying to use the breadcrumb functionality of the Html Helper in a Cake php application to create a breadcrumb trail. I followed the way they said to do it on the manual (scroll all the way to the bottom to see it), but the problem I am facing is that, when the view is loaded via Ajax (by using jquery's .load() function for example), my breadcrumb is not displayed. I put this in my view:

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

And this in my layout:

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

But nothing is displayed. When the view is not loaded via Ajax, everything is fine. Can anybody tell me why this is happening please?

Thank you

You 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
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM