how to add active class in current page in CakePhp

后端 未结 3 1485
醉梦人生
醉梦人生 2020-12-31 23:15

i have a problem similar to this question

How to identify active menu link in CakePHP

i have a page in my default.ctp file in which i want to add \'active\

相关标签:
3条回答
  • 2020-12-31 23:49

    Not to revive a dead post, but this is what I do (which I believe is a bit cleaner and faster and a bit more manageable)

    I create an element that has an array of pages, then I check against each item in the array to see if it is the current page. If it is I add the active class.

    I can then call this element from anywhere.

    // Changed the line below to a multi-dimensional array to cater for different controllers and actions
    
    //$mypages = array('Home','About','Pricing','FAQs','Contact');
    $mypages = array(
     array('controller'=>'controller1','action'=>'action1','name'=>'name1'),
     array('controller'=>'controller2','action'=>'action2','name'=>'name2
    ')
    );
    foreach ($mypages as $page ){
    // Changed to account for controller and action
    //$currentPage = isset($this->params['pass'][0]) ?$this->params['pass'][0] : "";
    $controller = isset($this->request->params['controller'])?$this->request->params['controller']: "";
    $action= isset($this->request->params['action'])?$this->request->params['action']: "";
    
        if (strtolower($page['controller']) == $controller && strtolower($page['action']) == $action) {  
            echo "<li class='active'>" . $this->Html->link($page,array("controller"=>"pages", "action"=>strtolower($page))) . "</li>" ;  
        } 
        else  {
            echo "<li>" . $this->Html->link($page,array("controller"=>"pages", "action"=>strtolower($page)))  . "</li>"; 
        }
    }
    
    0 讨论(0)
  • 2021-01-01 00:00

    If you have a different controller and you have declared a method with same name, and the above code is not working, then you can do the following:

    <li class="<?php echo (($this->params['controller']==='hotels')&& ($this->params['action']=='view') )?'active' :'' ?>" >
       <?php echo $this->Html->link('Hotels', array('controller' => 'hotels', 'action' => 'view')); ?>
    </li>
    
    <li class="<?php echo (($this->params['controller']==='packages')&& ($this->params['action']=='view') )?'active' :'' ?>" >
       <?php echo $this->Html->link('Packages', array('controller' => 'packages', 'action' => 'view')); ?>
    </li>
    

    Here view method is declared in different controller. i hope it will be helpful for you.

    0 讨论(0)
  • 2021-01-01 00:08

    This is a simple logic as follows

    <li class="<?php echo (!empty($this->params['action']) && ($this->params['action']=='controlpanel') )?'active' :'inactive' ?>">
      <?php echo $this->Html->link('Dashboard', array('controller'=>'users','action' => 'controlpanel'), array('title' => 'Dashboard','class' => 'shortcut-dashboard'));?>
    </li>
    
    <li class="<?php echo (!empty($this->params['action']) && ($this->params['action']=='index') )?'active' :'inactive' ?>">
      <?php echo $this->Html->link('Contacts', array('controller'=>'contacts','action' => 'index'), array('title' => 'Contacts','class' => 'shortcut-contacts'));?></li>
    
    0 讨论(0)
提交回复
热议问题