Steve
Steve

Reputation: 19

Active menu link with CAKEPHP

I am using CakePHP and I have a doubt.

  1. I have all my views in app/view/pages/
  2. I use url routing Router :: connect ('/', array ('controller' => 'I REMOVED THE WORD PAGES HERE', 'action' => 'index')); instead of
    Router :: connect ('/', array ('controller' => 'pages', 'action' => 'display', 'home'));
    This allows me to have www.site.com/contact instead of www.site.com/pages/contact
    Where I put "I REMOVED THE PAGES WORD HERE", this is only the single quotes.
  3. 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.

  4. I read over 30 different pages on the subject, but no code shown solved my problem. What I want is to highlight the active menu LI CLASS = "ACTIVE", that's all. If i'm in www.site.com/contact, my menu should highlight out that I'm on the 'contact' etc.

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

Answers (2)

scrowler
scrowler

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

karmicdice
karmicdice

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

Related Questions