I have a view called 'sub_nav' and it currently holds a multidimentional array of the links for each section. This view looks at the controller name to get the current section and then loops through the appropriate array set and outputs the links.
that works works but it feels like maybe I should create a controller just for the nav? and make sub_nav simpler but only outputing.... ? can anyone advice?
$controllerName = $this->router->class;
$methodName = $this->router->method;
$subLinks['about'] = array(
'introduction' => 'Introduction',
'people' => 'Our People'
);
$subLinks['contact'] = array(
'singapore' => 'Singapore',
'japan' => 'Japan'
);
?>
<ul>
<?php foreach($subLinks[$controllerName] as $link=>$linkName){ ?>
<li <?php if($methodName == $link){ ?>class="on"<? } ?>><a href="<?php echo base_url(); ?><?php echo $controllerName ?>/<?php echo $link ?>/"><?php echo $linkName ?></a></li>
<? } ?>
</ul>
`
If the content is a static array, I would put it in either:
- A view file. Controllers aren't the only place views can be loaded, there's nothing wrong with calling
$this->load->view()
from within another view file (your template). Just store the array and the view logic there.
- A config file. May sound strange, but it's a perfect place for static data like this. This way you won't have to constantly load the data in every load->view() call. Just load the config file in
MY_Controller
or something, and you have access to it anywhere. Then you can write a navigation view or library that will output whatever navigation array you send to it (in other words, don't do any html here - just data, then use for the config item in your view). I say to use a base controller because there's probably no need to autoload this data, for an AJAX request for instance. You won't always need it.
It definitely doesn't belong in a Controller, which is more for processing requests and data. A library is a more likely candidate. If the content was dynamic (from the database for example) then you'd definitely want to use a Model or Library, but in this case I would prefer a view file. It's not likely you'll be using the navigation array data anywhere else except your views.
In any case, HTML belongs in views whenever possible. If you only use the navigation array in views, only in one view, and it's not dynamic, just store it right there in the view file. Models and libraries should be reusable, storing static data there (that you'll probably need to update frequently) makes no sense to me, but I'd be willing to listen to opponents.
Depending where the data for the navigation comes from, I might give it a model of its own (if it comes from a database for example). As for the logic for choosing navigation to display, I would put that logic in a common base controller, either running it on all page loads or placing code for it in a method on the base controller, so those pages that need it can use it.
If your controllers use something like $this->data to hold the data to send to the view, then the base controller logic could pass the navigation data to the view too.