ZF2 Breadcrumbs Helper - How to render it as an unordered list?
Asked Answered
A

5

7

I'm new to Zend Framework2 and I´m asking for advice for the following situation:

I´m using the ZF2 Breadcrumbs Helper to create breadcrums on my project and this code:

 //breadcrumbs.phtml 

 echo $this->navigation('Navigation')
        ->breadcrumbs()
        ->setLinkLast(false)               // link last page
        ->setMaxDepth(7)                   // stop at level 7
        ->setMinDepth(0)                   // start at level 0
        ->setSeparator(' »' . PHP_EOL);    // separator with newline 

when rendered looks like this:

Home » Lorem Ipsum1 » Lorem Ipsum2 » Current Crumb

Exploring the source code:

<div id="breadcrumbs">
    <a href="/">Home</a>
    »
    <a href="/">Lorem Ipsum1</a>
    »
    <a href="/">Lorem Ipsum2</a>
    » Current Crumb
</div>

So, my question is: using the Breadcrumb Helper how can I get the following source code to be able to style the Breadcrumbs the way I want to?

<div id="breadcrumbs">
    <ul id="breadcrumbs">
       <li><a href="">Home</a></li>
       <li><a href="">Lorem Ipsum1</a></li>
       <li><a href="">Lorem Ipsum2</a></li>
       <li><a href="" class="current">Current Crumb</a></li>
    </ul>
</div>
Allonge answered 15/11, 2012 at 16:56 Comment(0)
C
8

You can use the view helper to set a partial to use:

<?php $partial = array('application/navigation/breadcrumbs.phtml', 'default') ?>
<?php $this->navigation('navigation')->breadcrumbs()->setPartial($partial) ?>
<?php echo $this->navigation('navigation')->breadcrumbs()->render() ?>

And then your partial would be something like this (application/navigation/breadcrumbs.phtml):

<?php $container = $this->navigation()->breadcrumbs()  ?>
<?php /* @var $container \Zend\View\Helper\Navigation\Breadcrumbs */ ?>
<?php $navigation = $container->getContainer() ?>
<?php /* @var $navigation \Zend\Navigation\Navigation */ ?>

<ul class="breadcrumb">
    <li><a href="<?php echo $this->url('soemroute') ?>">Home</a> <span class="divider">/</span></li>
    <?php foreach($this->pages as $page): ?>
        <?php /* @var $page \Zend\Navigation\Page\Mvc */ ?>
        <?php if( ! $page->isActive()): ?>
            <li>
                <a href="<?php echo $page->getHref() ?>"><?php echo $page->getLabel() ?></a> 
                <span class="divider">/</span>
            </li>
        <?php else: ?>
            <li class="active">
                <?php if($container->getLinkLast()): ?><a href="<?php echo $page->getHref() ?>"><?php endif ?>
                <?php echo $page->getLabel() ?>
                <?php if($container->getLinkLast()): ?></a><?php endif ?>
            </li>
        <?php endif ?>
    <?php endforeach ?>
</ul>
Cadaverous answered 8/2, 2013 at 9:49 Comment(0)
J
0

I'm not sure if this will help you but the progress should be similar like: Here

More info: zf2 documentation Rendering breadcrumbs using a partial view script

Jainism answered 15/11, 2012 at 17:1 Comment(2)
I´m using ZF2 Navigation Helper to create the Breadcrumbs. The example that you posted is relative to the way Breadcrumbs were made with the previous version of ZF right?Allonge
the stackoverflow link maybe, but the documentation is about zf2.Jainism
P
0

I just create a Twitter Bootstrap breadcrumb on my ZF2

breadcrumb.phtml

<?php
if (count($this->pages) > 0) :
?>
<ol class="breadcrumb">
<?php
for ($i = 0, $iend = count($this->pages); $i < $iend; $i++) :
    $a=$this->pages[$i];
    $label = $this->translate($a->getLabel());
?>
    <li <?php if ($i + 1 == $iend) : ?> class="active" <?php endif ?> >
        <?php if ($i + 1 != $iend) : ?>
            <a href="<?php echo $a->getHref(); ?>"><?php echo $label ?></a>
        <?php else : ?>
            <?php echo $label ?>
        <?php endif ?>
    </li>
<?php endfor ?>
</ol>
<?php endif ?>

And print on my template

<?php echo $this->navigation()->breadcrumbs('Navigation')->setPartial('partial/breadcrumb.phtml'); ?>
Pragmatism answered 12/11, 2014 at 18:45 Comment(0)
Z
0

I know this is an old question, but if someone wants a simple solution for render ul-li menu, you can use this code in your breadcrumbs.phtml:

<?php $breadcrumbs = $this->navigation()->breadcrumbs()->setMinDepth(0); ?>
<?php $items = array_filter(explode($breadcrumbs->getSeparator(), $breadcrumbs->render())); ?>
<?php if ($items) : ?>
    <ul class="breadcrumbs">
        <?php foreach ($items as $item) : ?>
            <li><?= $item ?></li>
        <?php endforeach; ?>
    </ul>
<?php endif; ?>
Zoes answered 10/8, 2018 at 7:41 Comment(0)
P
0

For ZF3

file: partial/breadcrumbs.phtml

<?php
/**
 * Zend breadcrumb partial
 *
 * @date 12.09.2018 16:47
 * @var $this \Zend\View\Renderer\PhpRenderer
 * @var $page \Zend\Navigation\Page\AbstractPage
 */

if (0 == count($this->pages)) {
    return;
}
?>
<ul class="breadcrumb">
    <?php foreach ($this->pages as $page) :?>
        <?php if ($page->isActive()): ?>
            <li class="active"><?= $page->getLabel() ?></li>
        <?php else : ?>
            <li><a href="<?= $page->getHref()?>"><?= $page->getLabel() ?></a></li>
        <?php endif; ?>
    <?php endforeach; ?>
</ul>

file: ./config/autoload/navigation.global.php

<?php
return [
    'navigation' => [
        'breadcrumb' => [
            [
                'label' => 'Home',
                'route' => 'home',
                'pages' => [
                    [
                        'label' => 'Page - 1',
                        'route' => 'doc',
                        'pages' => [
                            [
                                'label' => 'Sub page 2',
                                'route' => 'doc/page',
                                'params' => ['page' => 'control-panel'],
                            ],
                        ]
                    ],
                ]
            ],
        ],
    ],
];

On the view

<?= $this->navigation('Zend\Navigation\Breadcrumb')
         ->breadcrumbs()
         ->setPartial('partial/breadcrumbs.phtml')
?>
Pearman answered 12/9, 2018 at 12:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.