ZF2 Breadcrumbs Helper - 如何将其呈现为无序列表?

问题描述 投票:7回答:5

我是Zend Framework2的新手,我正在寻求以下情况的建议:

我正在使用ZF2 Breadcrumbs Helper在我的项目中创建面包棒和这段代码:

 //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 

渲染时看起来像这样:

首页> LOREM ipsum1»LOREM Ipsum2»电流克拉姆

探索源代码:

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

所以,我的问题是:使用Breadcrumb Helper如何获得以下源代码,以便能够按照我想要的方式设置Breadcrumbs样式?

<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>
php html5 zend-framework2 breadcrumbs view-helpers
5个回答
8
投票

您可以使用视图助手设置要使用的部分:

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

然后你的部分将是这样的(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>

0
投票

我不确定这对你有帮助,但进展应该类似:Here

更多信息:zf2 documentation使用部分视图脚本渲染面包屑


0
投票

我只是在我的ZF2上创建一个Twitter Bootstrap面包屑

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 ?>

并在我的模板上打印

<?php echo $this->navigation()->breadcrumbs('Navigation')->setPartial('partial/breadcrumb.phtml'); ?>

0
投票

我知道这是一个老问题,但如果有人想要一个简单的渲染ul-li菜单解决方案,你可以在你的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; ?>

0
投票

对于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>

文件:./ 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'],
                            ],
                        ]
                    ],
                ]
            ],
        ],
    ],
];

在视图上

<?= $this->navigation('Zend\Navigation\Breadcrumb')
         ->breadcrumbs()
         ->setPartial('partial/breadcrumbs.phtml')
?>
© www.soinside.com 2019 - 2024. All rights reserved.