Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Determine current item in navigation
  • Hi, I was trying to figure out how to determine the current item in a navigation and set it a special class. The only solution I've found is something like this: <li<?php if(Uri::current()==Uri::create('welcome')){echo ' class="selected"';}?>><?php echo Html::anchor('welcome', 'Home'); ?></li> I hope that I missed something and there's a better and easier way to achieve this, because this solution isn't the most elegant and moreover is pretty limited.
  • Try to use the Uri::segment() method.
    [url=http://website.local/controller/action]http://website.local/controller/action[/url]
    
    Uri::segment(1) -> "controller"
    Uri::segment(2) -> "action" 
    
    

    so ...
    <li<?php if(Uri::segment(1) == 'welcome') echo ' class="selected"'; ?>><?php echo Html::anchor('welcome', 'Home'); ?></li>
    
  • Davide Bellini wrote on Tuesday 9th of August 2011:
    Try to use the Uri::segment() method.
    [url=http://website.local/controller/action]http://website.local/controller/action[/url]
    
    Uri::segment(1) -> "controller"
    Uri::segment(2) -> "action" 
    
    

    so ...
    <li<?php if(Uri::segment(1) == 'welcome') echo ' class="selected"'; ?>><?php echo Html::anchor('welcome', 'Home'); ?></li>
    

    Thanks David for the quick reply, your method looks better indeed. I will try it... I would love to see something like this to generate a navigation and add the class selected to the current item.
    echo Html::nav(array('welcome', 'posts:create'));
    
  • Here's the start to a bit I have not quite finished yet... I don't even know if it works... but it's close at least. Let's see what you can come up with. http://pastecode.com/X9
  • Actually, this may be closer... I pasted the wrong branch from my repo. (again, untested as of yet) http://pastecode.com/Xt

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion