I have an interesting problem. I currently have a basic template library that renders out a bunch of modules for header and footer templates, then sandwiches a view I specify in between them.
Example:
$this->load->view('header.php', $headerstuff);
$this->load->view($contentView);
$this->load->view('footer.php', $footerstuff);
The problem is that I need to put some javascript (that is specific to each content view) into the header. I have been doing this with a switch statement containing the js inside the template library. But that makes no sense in the mvc model.
Example (of what I’ve been doing), (in template library, above previous code):
$headerstuff['js'] = '';
switch ($contentView)
{
case 'main':
$headerstuff['js'] = 'JAVASCRIPT INCLUDE CODE 1';
break;
case 'product':
$headerstuff['js'] = 'JAVASCRIPT INCLUDE CODE 2';
break;
}
I can’t think of another way to do this though. I would like to (ideally) store the js in a variable inside the content view file, and (somehow) load that into the header view. To be honest though, I don’t even think that is possible.
Does anybody have a better way of doing this then my current solution?
Thanks,
Max
I created a helper file to do this for my sites and I think we have a similar MVC template layout:
Asset Helper:
This does one of two things. I will allow you to add files in the controller file which is nice. I do this by creating a data object:
Then in your header include do the following:
I’m settings some defaults that will load on each page and then I can add in different files based on which controller I’m loading.
Hope this helps.