11

我目前正在从数据库中构建一个菜单,并且代码效果很好。但是,我现在想使用 twitter 引导程序设置菜单样式,这就是我遇到问题的地方。有谁知道将 zend 框架 2 导航菜单与 twitter 引导程序集成的方法?如果有人感兴趣,生成的菜单如下所示。

<ul class="nav">
<li>
    <a href="/view-page/home">Home</a>
    <ul>
        <li>
            <a href="/view-page/coupons">Coupons</a>
            <ul>
                <li>
                    <a href="/view-page/printable-coupons">Printable Coupons</a>
                    <ul>
                        <li>
                            <a href="/view-page/cut-these-coupons">Cut these here coupons</a>
                        </li>
                    </ul>
                </li>
            </ul>
        </li>
    </ul>
</li>
<li class="active">
    <a href="/view-page/about-us">About Us</a>
</li>
</ul>
4

4 回答 4

24

您可以根据需要使用部分生成导航。

要在模板中显示导航:

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

您的导航部分应该是这样的:

应用程序/导航/topnav.phtml

   <ul class="nav">
    <?php $count = 0 ?>
    <?php foreach ($this->container as $page): ?>
        <?php /* @var $page Zend\Navigation\Page\Mvc */ ?>
        <?php // when using partials we need to manually check for ACL conditions ?>
        <?php if( ! $page->isVisible() || !$this->navigation()->accept($page)) continue; ?>
        <?php $hasChildren = $page->hasPages() ?>
        <?php if( ! $hasChildren): ?>
        <li <?php if($page->isActive()) echo 'class="active"'?>>
            <a class="nav-header" href="<?php echo $page->getHref() ?>">
                <?php echo $this->translate($page->getLabel()) ?>
            </a>
        </li>
        <?php else: ?>
        <li class="dropdown">
            <a class="dropdown-toggle" data-toggle="dropdown" href="#">
                <span><?php echo $this->translate($page->getLabel()) ?></span>
            </a>

            <ul class="dropdown-menu" id="page_<?php echo $count ?>">
            <?php foreach($page->getPages() as $child): ?>
                <?php // when using partials we need to manually check for ACL conditions ?>
                <?php if( ! $child->isVisible() || !$this->navigation()->accept($child)) continue; ?>
                <li>
                    <a href="<?php echo $child->getHref() ?>">
                        <?php echo $this->translate($child->getLabel()) ?>
                    </a>
                </li>
            <?php endforeach ?>
            </ul>
         </li>   
        <?php endif ?>
        <?php $count++ ?>
    <?php endforeach ?>
</ul>

显然,这是一个简单的示例,不会处理任意数量的导航级别,并且您需要添加一些额外的类名等以使其与 Bootstrap 完美配合,但您明白了。

于 2013-02-15T09:42:00.757 回答
5

对于新的 Bootstrap 3,将其用作局部视图;

<nav class="navbar navbar-inverse navbar-fixed-top" role="navigation">
<div class="container">
    <div class="navbar-header">
        <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
        </button>
        <a class="navbar-brand" href="<?php echo $this->url('home') ?>"><img src="<?php echo $this->basePath('img/logo.png') ?>" alt="Title App"/>&nbsp;TitleText</a>
    </div>
    <div class="collapse navbar-collapse">
        <ul class="nav navbar-nav">
        <?php foreach ($this->container as $page) { ?>
            <?php /* @var $page Zend\Navigation\Page\Mvc */ ?>
            <?php // when using partials we need to manually check for ACL conditions ?>
            <?php if (!$page->isVisible() || !$this->navigation()->accept($page)) { continue; } ?>
            <?php $hasChildren = $page->hasPages(); ?>
            <?php if (!$hasChildren) { ?>
                <li>
                    <a href="<?php echo $page->getHref() ?>">
                        <?php echo $this->translate($page->getLabel()) ?>
                    </a>
                </li>
            <?php } else { ?>
            <li class="dropdown">
                <a href="#" class="dropdown-toggle" data-toggle="dropdown"><?php echo $this->translate($page->getLabel()) ?> <b class="caret"></b></a>
                <ul class="dropdown-menu">
                    <?php foreach ($page->getPages() as $child) { ?>
                        <?php // when using partials we need to manually check for ACL conditions ?>
                        <?php if(!$child->isVisible() || !$this->navigation()->accept($child)) { continue; } ?>
                        <li>
                            <a href="<?php echo $child->getHref() ?>">
                                <?php echo $this->translate($child->getLabel()) ?>
                            </a>
                        </li>
                    <?php } ?>
                </ul>
            </li>
            <?php } ?>
        <?php } ?>
    </ul>
    </div>
</div>
</nav>
于 2014-02-16T21:24:53.400 回答
0

试试下面的代码;您的导航部分应该是这样的:

应用程序/部分/topnav.phtml

<div class="navbar navbar-inverse navbar-fixed-bottom">
  <div class="navbar-inner">
    <div class="container">
      <a class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse">
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </a>
      <ul class="nav" id="menu">    
        <?php $count = 0; ?>
        <?php foreach ($this->container as $page): ?>
            <?php //var_dump($page); exit;?>
            <?php /* @var $page Zend\Navigation\Page\Mvc */ ?>
            <?php // when using partials we need to manually check for ACL conditions ?>
            <?php if( ! $page->isVisible() || !$this->navigation()->accept($page)) continue; ?>
            <?php $hasChildren = $page->hasPages() ?>
            <?php if( ! $hasChildren): ?>
          <li>
                <a class="nav-header" href="<?php echo $page->getHref() ?>">
                  <?php echo $this->translate($page->getLabel()) ?>
                </a>
          </li>
            <?php else: ?>
                <a class="dropdown-toggle" data-toggle="dropdown" href="#page_<?php echo $count; ?>">
                    <span><?php echo $this->translate($page->getLabel()) ?></span>
                </a>
                <ul class="dropdown-menu" id="page_<?php echo $count; ?>">
                <?php foreach($page->getPages() as $child): ?>
                    <?php // when using partials we need to manually check for ACL conditions ?>
                    <?php if( ! $child->isVisible() || !$this->navigation()->accept($child)) continue; ?>
                    <li>
                        <a href="<?php echo $child->getHref() ?>">
                            <?php echo $this->translate($child->getLabel()) ?>
                        </a>
                    </li>
                <?php endforeach ?>
                </ul>
            <?php endif ?>
            <?php $count++; ?>
        <?php endforeach ?>
      </ul>
        <form class="navbar-search pull-right" action="">
          <input type="text" class="search-query span2" placeholder="Search">
        </form>
      </div><!-- /.nav-collapse -->
    </div><!-- /.container -->
  </div><!-- /.navbar-inner -->
</div><!-- /.navbar -->

要在模板中显示导航:

    <div class="nav-collapse">
      <?php 
          echo $this->navigation('navigation')
          ->menu()
          ->setMinDepth(0)
          ->setMaxDepth(0)
          ->setUlClass('nav')
          ->setPartial(array('partial/topnav.phtml', 'Application'));
      ?>
    </div>
于 2013-05-19T20:57:11.983 回答
0

要将外观与 zend nav-bar 集成,请更改以下内容:

<ul class="nav">

为了这:

<ul class="nav navbar-nav">

于 2014-11-04T16:35:58.250 回答