Zend Framework 2导航菜单Twitter Bootstrap集成

时间:2013-02-14 21:41:53

标签: css twitter-bootstrap zend-framework2

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

<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 个答案:

答案 0 :(得分:24)

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

要在模板中显示导航:

<?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完美配合,但你明白了。

答案 1 :(得分: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>

答案 2 :(得分: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>

答案 3 :(得分:0)

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

<ul class="nav">

为此:

<ul class="nav navbar-nav">