Reputation: 19
I am using CakePHP and I have a doubt.
In my menu, I have the following HTML (app/view/layouts/default.ctp):
href="contact">Contact
href="about">About
Above, the aim was to show the UL LI but I do not know how to expose code here.
In PHP I can do, but in CakePHP can not.
Could someone please help me? Thank you.
This is an exemple of what i'm talking about:
how to add active class in current page in CakePhp
But in my case, this is just a simples aplication. I don't use database and i don't use no any controller. I just use layout stuffs.
Upvotes: 0
Views: 6581
Reputation: 24406
Have you seen the MenuHelper on the Bakery? It allows you to let Cake apply those classes for you without having to worry about it:
http://bakery.cakephp.org/articles/alkemann/2009/02/04/menuhelper
If you're going to stick with the manual way, I'd suggest doing what Aryan has posted, but define your action variable first to save space and readability. In this example I've used the ternary operator:
<?php
$action = !empty($this->params['action']) ? $this->params['action'] : null;
?>
...
<li class="<?=($action == "home) ? 'active' : ''?>">...
Upvotes: 0
Reputation: 1061
you can know currenty on which page you are by $this->params['action'];
, so you use can this this to set active class either in <li>
or in <a>
<ul id="selectnav">
<li class="<?php echo (!empty($this->params['action']) && ($this->params['action']=='home') )?'active' :'inactive' ?>">
<a href="/"><i class="icon-hdd"></i>Home</a>
</li>
<li class="<?php echo (!empty($this->params['action']) && ($this->params['action']=='about') )?'active' :'inactive' ?>">
<a href="/aboutus"><i class="icon-heart-empty"></i>About</a>
</li>
<li class="<?php echo (!empty($this->params['action']) && ($this->params['action']=='contact') )?'active' :'inactive' ?>">
<a href="/contactus"><i class="icon-envelope-alt"></i>Contact</a>
</li>
</ul>
OR
<ul id="selectnav">
<li class="<?php echo (!empty($this->params['action']) && ($this->params['action']=='home') )?'active' :'' ?>">
<a href="/"><i class="icon-hdd"></i>Home</a>
</li>
<li class="<?php echo (!empty($this->params['action']) && ($this->params['action']=='about') )?'active' :'' ?>">
<a href="/aboutus"><i class="icon-heart-empty"></i>About</a>
</li>
<li class="<?php echo (!empty($this->params['action']) && ($this->params['action']=='contact') )?'active' :'' ?>">
<a href="/contactus"><i class="icon-envelope-alt"></i>Contact</a>
</li>
</ul>
The question you referred was right, you can use this method
Upvotes: 5