Zend 框架:使用 BreadCrumbs 和部分

发布于 2024-12-23 13:23:36 字数 2148 浏览 2 评论 0原文

我在制作定制面包屑时遇到一些麻烦: 我的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 调用得很好,但我不知道如何在我的BreadCrumb.phtml 中制作ul-li 菜单。如何获取我实际所在的导航? 预先感谢您的任何帮助。安德里亚

I have some trouble making a customized breadcrumb:
I my layout.phtml I have:

$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'
                        )
                    )
                )
            )
        )
    )
)
);

The next line is:

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

The BreadCrumb.phtml is called well, but I don't know how to make an ul-li menu in my BreadCrumb.phtml. How do I get the navigation I'm actually in?
Thanks in advance for any help. Andrea

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

痴意少年 2024-12-30 13:23:36

为此,您的 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>

To do so, your BreadCrumb.phtml should look like this:

<?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 : '';
?>

Then you will get something like this:

<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>
匿名的好友 2024-12-30 13:23:36

在视图脚本内,可通过 $this->container 检索 zend 导航容器。容器是可迭代的,因此您可以使用简单的 foreach 循环遍历它(例如 foreach($this->container as $page)

Inside the view script, the zend navigation container is retrievable via $this->container. The container is iterable, so you can walk through it with a simple foreach loop (e.g. foreach($this->container as $page)

嘦怹 2024-12-30 13:23:36

我找到了渲染面包屑包裹在 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; ?>

I've found simple solution for render breadcrumbs wrapped in ul-li menu.

You can place 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; ?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文