Neovive - 07 June 2007 08:43 AM
I have one shared header partial view that is used by all controllers that loads all standard CSS and JS, however, one of my controllers utilizes additional CSS and JS files for Lightbox functionality. Is there an easy way to use your View library to dynamically load a group of additional HEAD content for specific controllers, while still using one shared header.php partial view.
Are you using the linkCSS() and linkJS() methods of the View library? Here what I would do.
Have a single header.php file that is included by all controllers. It would contain all the links to CSS and JS that are common to all or most pages. No need to dynamically add those to the view since they are always needed.
header.php
<head>
<title>My Fancy Website</title>
<!-- include common stuff in here -->
<link type="text/css" href="/css/common_styles.css" />
<scrpt type="text/css" href="/js/common_functions.js"></scrpt>
<!-- variables for dynamic stuff -->
<?php echo $view_css; ?>
<?php echo $view_js; ?>
</head>
Then, in your controllers, you might have this:
$this->view->part('header', 'header.php');
You’d be assigning the contents of the header.php file to the $header variable in your template. So far, so good.
Because header.php contains variables for CSS and JS, your controllers can add whatever they want. The order you do it is irrelevant (as long as you have the latest version of the View library).
So, in your controller’s constructor you might set some CSS and JS common to all or most functions of that controller. Doing it in the constructor is just one solution when multiple functions need to do the same thing.
$this->view->linkCSS('/css/first_styles.css');
$this->view->linkCSS('/css/second_styles.css');
$this->view->linkJS('/js/some_functions.js');
I’m not familiar enough with Lightbox, but if it had some CSS and JS needed, it could also link to the necessary file:
$CI =& get_instance();
$CI->view->linkCSS('/css/lightbox_styles.css');
$CI->view->linkJS('/js/lightbox_functions.js');
And then, when you’re ready to render your finished page, you simply call:
$this->view->load('template_name');
And everything is compiled and assigned to the view in one fell swoop.
Does that help?