Welcome Guest, Not a member yet? Register   Sign In
display_helper : makes links active according to current controller - very tiny piece of code
#1

[eluser]benoa[/eluser]
Hello, I wanted to share this tiny helper that I called display_helper.php, that contains only one function at the moment, that is doing one thing : return a css class suffix (by default "-active") when the current controller (uri->segment(1) by default) is the one provided as a parameter.

Here's the code :
Code:
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
* Display Helper - active()
*
*  Returns a css class suffix if the
*    current controller is the one provided as $uri / $level
*/    
if ( ! function_exists('active'))
{
    function active($uri, $level = 1, $class_suffix = '-active')
    {
        $ci =& get_instance();
        if ($ci->uri->segment($level) == $uri)
        {
            return $class_suffix;
        }
        else return;
    }
}

/* End of file display_helper.php */
/* Location: ./system/helpers/display_helper.php */

How to use :
Code:
<div id="nav">
            <ul>
                <li class="highlight"><a href="&lt;?php echo site_url('home'); ?&gt;" class="home&lt;?php echo active('home'); ?&gt;"><span>Home</span></a></li>
                <li class="highlight"><a href="&lt;?php echo site_url('booking'); ?&gt;" class="booking&lt;?php echo active('booking'); ?&gt;"><span>Online booking</span></a></li>
                <li class="highlight"><a href="&lt;?php echo site_url('about'); ?&gt;" class="about&lt;?php echo active('about'); ?&gt;"><span>About</span></a></li>
                <li class="highlight"><a href="&lt;?php echo site_url('contact'); ?&gt;" class="contact&lt;?php echo active('contact'); ?&gt;"><span>Contact</span></a></li>
                <li class="highlight"><a href="&lt;?php echo site_url('partners'); ?&gt;" class="partners&lt;?php echo active('partners'); ?&gt;"><span>Partners</span></a></li>
            </ul>
            <div class="clear"></div>
        </div>&lt;!-- End #nav --&gt;

It is a very tiny little piece of nothing code but almost every website needs it so I thought it could be good for some people around Smile

If you have any comments about it, I'm all ears!

Have a nice day!
B
#2

[eluser]trice22[/eluser]
Hello and thanks for the contribution.

From my perspective I'd rather go with a body ID/class, though for this purpose.
http://css-tricks.com/id-your-body-for-g...ecificity/

—trice
#3

[eluser]benoa[/eluser]
Thank you Trice, that's a smarter approach I wasn't aware of. I'll embrace it in my next project Smile
#4

[eluser]Philipp Gérard[/eluser]
For all those considering to use this implementation: Don't. It's inflexible as hell, better write yourself a helper that uses anchor() and dynamically adds a subclass if the controller/action is active. I'll post my implementation once I've written it.
#5

[eluser]jeanv[/eluser]
very useful! Thanks for sharing
#6

[eluser]benoa[/eluser]
@jeanv : you're welcome Smile

@Philipp Gérard : your idea sounds interesting... Any code yet?
#7

[eluser]Philipp Gérard[/eluser]
Hey benoa,

I didn't have the chance to write the function properly, but the following (from scratch) works and surely shows what I meant. However, I am not 100% sure if it works properly in every environment (w/wo .htaccess etc.), needs more testing. Any feedback appreciated!

Best wishes,
Philipp

Code:
&lt;?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

if ( ! function_exists('active_anchor'))
{
    /**
     * Provides the same functionality as anchor() (part of the default url-helper from CI)
     * but adds class="active" (or subclass 'active' to existing classes) to make it easy
     * to flag navigation elements using CSS.
     *
     * @param string or array $path
     * @param string $text
     * @param array $attributes
     * @return string
     * @author Philipp Gérard
     */
    function active_anchor($path = '', $text = '', $attributes = array())
    {
        $CI =& get_instance();
        
        if(!is_array($path) AND is_string($path))
        {
            $path = explode('/', $path);
        }
        if(count($path) == 1 AND $path[0] == '')
        {
            return anchor($path, $text, $attributes);
        }
        else
        {
            $active = NULL;
            $segments = $CI->uri->segment_array();
            for($i = 1; $i < count($path); $i++)
            {
                if(isset($segments[$i]))
                {
                    if($segments[$i] == $path[$i])
                    {
                        $active = TRUE;
                    }
                    else
                    {
                        $active = FALSE;
                        break;
                    }
                }
            }
            if($active === TRUE)
            {
                if(!isset($attributes['class']) OR empty($attributes['class']))
                {
                    $attributes['class'] = 'active';
                }
                else
                {
                    $attributes['class'] .= ' active';
                }
            }
            return anchor($path, $text, $attributes);
        }
    }
}

?&gt;

Usage:

Code:
&lt;?= active_anchor('/user/login', 'Login'); ?&gt;

Returns:

Code:
<a href="http://yourdomain.com/user/login.html" class="active">Login</a>
#8

[eluser]benoa[/eluser]
It looks like a good solution. I have to try it when I got a chance.

It feels like a lot of code though, for this small functionality. I'd rather use the body class instead.
#9

[eluser]Philipp Gérard[/eluser]
Well, sum up the useless function calls in your views and you'll end up with much more unwanted code Smile




Theme © iAndrew 2016 - Forum software by © MyBB