Zend Framework: использование BreadCrumbs с частичным - PullRequest
1 голос
/ 02 января 2012

У меня возникли проблемы с приготовлением крошки: У меня есть layout.phtml:

$container = new Zend_Navigation();
$this->navigation($container);

$container->addPage(
array(
    'label'      => 'Dashboard',
    'module'     => 'default',
    'controller' => 'dashboard',
    'action'     => 'index',
    'pages'      =>
    array(
        array(
            'label'      => 'Create Order',
            'module'     => 'default',
            'controller' => 'createorder',
            'action'     => 'index'
        ),
        array(
            'label'      => 'Query',
            'module'     => 'default',
            'controller' => 'query',
            'action'     => 'index',
            'pages' => array(
                array(
                    'label'      => 'View Order',
                    'module'     => 'default',
                    'controller' => 'order',
                    'action'     => 'vieworder'
                )
            )
        ),
        array(
            'label'      => 'Administration',
            'module'     => 'default',
            'controller' => 'admin',
            'action'     => 'index',
            'pages'      =>
            array(
                array(
                    'label'      => 'News and Announcements',
                    'module'     => 'default',
                    'controller' => 'admin',
                    'action' => 'addnews',
                    'pages' => array(
                        array(
                            'label'      => 'Edit News and Announcements',
                            'module'     => 'default',
                            'controller' => 'admin',
                            'action' => 'editnews'
                        )
                    )
                )
            )
        )
    )
)
);

Следующая строка:

echo $this->navigation()->breadcrumbs()->setPartial(array('BreadCrumb.phtml','default'));

Файл BreadCrumb.phtml называется хорошо, но я не знаю, как создать меню ul-li в моем файле BreadCrumb.phtml. Как мне получить навигацию, в которой я на самом деле? Заранее благодарю за любую помощь. Andrea

Ответы [ 3 ]

4 голосов
/ 17 января 2012

Для этого ваш BreadCrumb.phtml должен выглядеть следующим образом:

<?php
if (null === $this->container) {
    $this->container = $this->breadcrumbs()->getContainer();
}

// find deepest active
if (!$active = $this->breadcrumbs()->findActive($this->container)) {
    return '';
}

$active = $active['page'];

// put the deepest active page last in breadcrumbs
if ($this->breadcrumbs()->getLinkLast()) {
    $html = ' <li>' . $this->breadcrumbs()->htmlify($active) . '</li>' . PHP_EOL;
} else {
    $html = $active->getLabel();
    if ($this->breadcrumbs()->getUseTranslator() && $t = $this->breadcrumbs()->getTranslator()) {
        $html = $t->translate($html);
    }
    $html = ' <li>' . $this->escape($html) . '</li>' . PHP_EOL;
}

// walk back to root
while (($parent = $active->getParent()) != null) {
    if ($parent instanceof Zend_Navigation_Page) {
        // prepend crumb to html
        $html = ' <li>' . $this->breadcrumbs()->htmlify($parent) . '</li>' . PHP_EOL . $html;
    }

    if ($parent === $this->container) {
        // at the root of the given container
        break;
    }

    $active = $parent;
}
echo strlen($html) ? $this->breadcrumbs()->getIndent() . '<ul id="breadcrumbs">' . PHP_EOL 
                 . $html .  '</ul>' . PHP_EOL : '';
?>

Тогда вы получите что-то вроде этого:

<ul id="breadcrumbs">
    <li><a href="/home">Home</a></li>
    <li><a href="/articles">Articles</a></li>
    <li><a href="/shop">Shop</a></li>
    <li>Last link</li>
</ul>
0 голосов
/ 10 августа 2018

Я нашел простое решение для рендеринга панировочных сухарей в меню 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 голосов
/ 06 января 2012

Внутри скрипта представления контейнер навигации Zend можно получить через $this->container.Контейнер является итеративным, поэтому вы можете пройти по нему с помощью простого цикла foreach (например, foreach($this->container as $page)

...