[eluser]philpem[/eluser]
Hi guys,
I'm doing some work on an application that has a navigation bar and menu along the top of each page, and a version number string on the bottom. All of these should be dynamic -- i.e. on the "Manufacturers" page, you get "List Mfgrs / Add New Mfgr / Search Mfgrs" as a submenu, the "Manufacturers" menu item is highlighted in the main menu, etc.
This is all view-related code, and I have a pretty good idea how to do it -- pass the current menu and submenu name into the view from the controller, and use that in the view to decide which menu items to highlight.
Thing is, I don't want to have code like this:
Code:
$data = array();
$data['currentMenu'] = 'foo';
$data['currentSubMenu'] = 'bar';
// ...
// main code adds stuff to $data too
$this->load->view('dashboard', $data);
... in all of my controller functions -- it's messy. Setting the submenu I can deal with, but the menu name is going to be static for an entire controller. The version number is system-global, so I'd like to add that to either global initialisation code, or maybe the ctor for each controller (though I'd consider the latter to be a pretty messy solution).
Now with libraries like Smarty, there's an "assign" function, which assigns a value to a template variable:
Code:
$sm->assign("fred", "This is the variable fred");
Which means that I could just put a call or three to assign() in the controller class constructor and my problem would be solved fairly cleanly.
But as I understand it, there isn't an equivalent in CodeIgniter -- you always have to pass the data for each view into the $this->load->view() call. There doesn't seem to be a way to do what I want -- that is, assign a couple of variables before the load->view() call, then load the rest when load->view() is called.
The nearest I have come to a workaround is:
Code:
class GenController {
function __construct() {
$this->$tdata = array();
$this->$tdata['VERSION'] = "0.01pre1";
$this->$tdata['CURMENU'] = "GeneralStuff";
}
function DoSomething() {
$this->$tdata['fred'] = "this is fred";
$this->$tdata['jim'] = "this is jim";
$this->load->template("page", $this->$tdata);
}
}
Is there a better way to do this?
Thanks,
Phil.