⚠ In case you've missed it, we have migrated to our new website, with a brand new forum. For more details about the migration you can read our blog post for website migration. This is an archived forum. ⚠

  •     

profile picture

Easy set_active_nav() for GC layout



preg
  • profile picture
  • Member

Posted 08 January 2014 - 14:43 PM

Hi all,

 

Today is my first topic. I use Grocery since few months in web agency. i really appreciate this CRUD that's why i'll try to help the community.

Here is a modification that improve the set_active_nav function.

 

Just see that exemple to understand my search.

 

Before :

<li<?= set_active_nav(array('home/credit','home/credit/add')) ?>>
<?= anchor('home/credit', '<i class="icon-list-alt"></i> Credit') ?>
</li>

And i wasn't able to active my link on an edit because the url was dynamic "edit/2".

 

Now :

<li<?= set_active_nav('home/credit') ?>>
<?= anchor('home/credit', '<i class="icon-list-alt"></i> Credit') ?>
</li>

This is not a big evolution ^^ but that could be usefull.

 

To dot that we have to modify that file : navigation_helper.php on : application/helpers folder.

 

You will open this file :

<?php
/**
 * Codeigniter Bootstrap
 * -------------------------------------------------------------------
 * Developed and maintained by Stijn Geselle <stijn.geselle@gmail.com>
 * -------------------------------------------------------------------
 */

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

if (!function_exists('set_active_nav')) {
    function set_active_nav($uri)
    {
        return is_current_uri($uri) ? ' class="active"' : '';
    }
}

if (!function_exists('is_current_uri')) {
    function is_current_uri($uri)
    {
        $CI =& get_instance();
        $curr_uri = $CI->uri->uri_string();
        
        return is_array($uri) && in_array($curr_uri, $uri) || $curr_uri == $uri;
    }
}

/* End of file navigation_helper.php */
/* Location: ./application/helpers/navigation_helper.php */

my modification consist to use regEx to find our current path (main path).

 

Modified file :

<?php
/**
 * Codeigniter Bootstrap
 * -------------------------------------------------------------------
 * Developed and maintained by Stijn Geselle <stijn.geselle@gmail.com>
 * -------------------------------------------------------------------
 */

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

if (!function_exists('set_active_nav')) {
    function set_active_nav($uri)
    {
        return is_current_uri($uri) ? ' class="active"' : '';
    }
}

if (!function_exists('is_current_uri')) {
    function is_current_uri($uri)
    {
        $CI =& get_instance();
        $curr_uri = $CI->uri->uri_string();
        
        return is_array($uri) && in_array($curr_uri, $uri) || 
            $curr_uri == $uri || preg_match('#^'.$uri.'\/\w+#', $curr_uri) && substr_count($uri,'/') > 0;
    }
}

/* End of file navigation_helper.php */
/* Location: ./application/helpers/navigation_helper.php */

Hope i help someone with my 'TIP' :).

 

Ps : Sorry for my english, i'm not very good. French touch :P