how to add active class in current page in CakePhp

后端 未结 3 1501
醉梦人生
醉梦人生 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 "
  • " . $this->Html->link($page,array("controller"=>"pages", "action"=>strtolower($page))) . "
  • " ; } else { echo "
  • " . $this->Html->link($page,array("controller"=>"pages", "action"=>strtolower($page))) . "
  • "; } }

提交回复
热议问题