如何在 CakePhp 的当前页面中添加活动类

2021-12-21 00:00:00 php css cakephp cakephp-2.0 cakephp-2.1

我有一个类似这个问题的问题

i have a problem similar to this question

如何在 CakePHP 中识别活动菜单链接

我的 default.ctp 文件中有一个页面,我想在其中添加活动"类的链接.我如何识别页面的当前 url,然后在链接上应用该类.. 我也在那里遵循了

i have a page in my default.ctp file in which i want to add 'active' class on links. how can i identify the current url of the page and then apply the class on link.. i have followed the answer also there which is

      $url = $this->Html->url('INPUT_THE_URL') ;
     $active = $this->request->here == $url? true: false;

我不知道如何在我的代码中做到这一点..很抱歉我是cakephp的新手..这是我的代码

i dont know how can i do this in my code .. sorry for asking as i am newbie in cakephp .. here is my code

 **default.ctp file** 

 <li>
      <?php echo $this->Html->link('Dashboard', array('controller'=>'users','action' => 'controlpanel'), array('title' => 'Dashboard','class' => 'shortcut-dashboard'));?></li>



  <li> <?php echo $this->Html->link('Contacts', array('controller'=>'contacts','action' => 'index'), array('title' => 'Contacts','class' => 'shortcut-contacts'));?></li>

我想像这样添加一个带有 li 的类

i want to add a class with li like this

   <li class = 'active''>

推荐答案

这是一个简单的逻辑如下

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>

相关文章