I'm working on a site where there can be up to 3 levels of hierarchy in the navigation.
- main sections in the top header
- sub-navigation on the left
- optional sub-sub navigation below that
In the past, when I've rolled my own with PHP, I would create a file navigation.php holding a class and arrays for all the sections and sub sections and a couple of functions to output the navigation. I'd set variables on every page in the site (current_section ='', current_sub_section='') so the navigation function would know what to highlight.
In CodeIgniter, I'm wondering if this is still a good approach to use?
The best way is to use 3 different views, a top view, a left view and a bottom view then in your controller you can pass the apporiate vars to each view so you would do something like
Then in your views you can handle the variables passed to them.
I'm going to assume that your main nav sections almost directly map to your controllers, ie.
Home | News | Events
In your top bar, maps to:
If this is the case, you already have a simple way of selecting your nav array.
You can put an array of nav item-link pairs in your controller constructor & pass them along to a sub-view in your output.
Example:
Now what you've done is auto-registered a variable in all your views
$NavigationArray
that contains an array of Display Name - Link pairs.You can then load a basic Navigation View that builds up your subnav from that variable (as it's availiable everywhere).
And below that you can look for the existance of a sub-nav array that you could optionally set in your controller, or whatever (the third optional nav you talked about)
Please remember, this example is very basic, but this is generally how we pass data around, I wouldn't want you to put global variables anywhere and try to queue off them. Simply "load" variables into the view engine, and they'll be availiable when you go to render your views/subviews.
This way the controller controls what nav items get displayed.
Also note:
You can pass the variables explicitly instead of trusting they will exist in the scope of your view.
Hope this helps.
Here's basically what I do to determine "active" links in Codeigniter:
Keep in mind this is a basic example and usually run in a loop. The best way depends on what your navigation array looks like, and what you consider to be "active" (f you want to "activate" links whose href partially match the url).