2014-07-15 28 views
1

layout.phtml如何添加CSS類(活動),同時改變頁面Zend框架的佈局

<nav class="navbar navbar-inverse navbar-fixed-top" role="navigation"> 
      <div class="container"> 
       <div class="collapse navbar-collapse"> 
        <ul class="nav navbar-nav"> 
         <li class="active"><a href="<?php echo $this->url('login') ?>"><?php echo $this->translate('Home') ?></a></li> 

         <li class="dropdown"> 
    <a href="#" class="dropdown-toggle" data-toggle="dropdown" data-hover="dropdown" data-delay="50" data-close-others="false"> 
     Jobs <b class="caret"></b> 
    </a> 
    <ul class="dropdown-menu"> 
     <li><a tabindex="-1" href="<?php echo $this->url('home') ?>">Interviews</a></li> 
     <li><a tabindex="-1" href="#">Resume Tips</a></li> 
     <li><a tabindex="-1" href="#">Interview Ques</a></li> 
    </ul> 
</li> 
       <li class="" id="com"><a href="<?php echo $this->url('company') ?>"><?php echo $this->translate('HR Desk') ?></a></li> 
       <li class=""><a href="<?php echo $this->url('home') ?>"><?php echo $this->translate('Job Tips') ?></a></li>  
       <li class=""><a href="<?php echo $this->url('home') ?>"><?php echo $this->translate('Contact Us') ?></a></li> 

        </ul> 
       </div><!--/.nav-collapse --> 
      </div> 
     </nav> 

什麼,在哪裏應該怎麼做更改類時更改頁面添加CSS類(主動)

回答

0

有2種方式PHP和JavaScript:

PHP你必須讓onBootStrap方法該視圖並添加一個變量,如:「selectedSection」,然後在每個視圖上檢查是否==然後在每個li的class屬性上回顯活動。

否則,在每個視圖的末尾添加一個jquery像

$('li#com').addClass('active'); 

+1

謝謝..它的工作...非常感謝 – baladpi

0

解決方案1: 鑑於頁設置活動變量從控制器和顯示

In controller: 
    $this->layout()->setVariable('login_active', 'active'); 
This $login_active will get in layout 

HTML

<nav class="navbar navbar-inverse navbar-fixed-top" role="navigation"> 
    <div class="container"> 
     <div class="collapse navbar-collapse"> 
      <ul class="nav navbar-nav"> 
       <li class="<?php echo $this->login_active; ?>"><a href="<?php echo $this->url('login') ?>"><?php echo $this->translate('Home') ?></a></li> 

       <li class="dropdown <?php echo $this->view->job_active; ?>"> 
        <a href="#" class="dropdown-toggle" data-toggle="dropdown" data-hover="dropdown" data-delay="50" data-close-others="false"> 
         Jobs <b class="caret"></b> 
        </a> 
        <ul class="dropdown-menu"> 
         <li><a tabindex="-1" href="<?php echo $this->url('home') ?>">Interviews</a></li> 
         <li><a tabindex="-1" href="#">Resume Tips</a></li> 
         <li><a tabindex="-1" href="#">Interview Ques</a></li> 
        </ul> 
       </li> 
       <li class="<?php echo $this->company_active; ?>" id="com"><a href="<?php echo $this->url('company') ?>"><?php echo $this->translate('HR Desk') ?></a></li> 
       <li class="<?php echo $this->home_active; ?>"><a href="<?php echo $this->url('home') ?>"><?php echo $this->translate('Job Tips') ?></a></li>  
       <li class="<?php echo $this->another_page_active; ?>"><a href="<?php echo $this->url('home') ?>"><?php echo $this->translate('Contact Us') ?></a></li> 

      </ul> 
     </div><!--/.nav-collapse --> 
    </div> 
</nav> 

解決方案2: 從視圖中比較控制器的名稱,並指定活動類

In view: 
$controller = Zend_Controller_Front::getInstance()->getRequest()->getControllerName(); 
$action = Zend_Controller_Front::getInstance()->getRequest()->getActionName(); 
+0

但佈局在視圖之前。所以如果你設置視圖級別的變量,你將永遠不會得到它在佈局級別:) – peterpeterson

+0

你試過嗎? –

+0

我還沒有,但是我不得不在前段時間在zf2上傳遞一個變量菜單到佈局中,我無法從佈局訪問控制器變量。希望你是對的:) – peterpeterson