[eluser]Unknown[/eluser]
Hello
Problem:
I need to implement some sort of themes for a site I'm working on.
Solution:
Extend the CI_Loader class and overwrite the "view" function, to add an optional subdirectory for my views. Something like:
Code:
application/themes/mytheme/view.php
Extending the CI_Loader class would be simple, but we have a problem: the CI_Controller class extends CI_Loader and somewhere in the code we have:
Code:
$this->load =& $this; //to allow $this->load->view(...) to work
So, $this->load isn't an instance of the CI_Loader class (that we might have overwritten), but it's a reference to the controller object.
The solution is simple: between CI_Controller and our app controllers, we will add a new Controller class, that will overwrite the "view" function. The result will look like:
CI_Loader -> CI_Controller -> MyBaseController -> App Controller.
Code:
<?php
// application/controllers/MyControllerBase.php
class MyControllerBase extends Controller {
var $theme = NULL;
function view($view, $vars = array(), $return = FALSE, $fixored = FALSE)
{
$folder = '';
if (!is_null($this->theme)) {
$folder = 'themes/'.$this->theme.'/';
}
return $this->_ci_load(array('view' => $folder.$view, 'vars' => $this->_ci_object_to_array($vars), 'return' => $return));
}
}
?>
and an application controller:
Code:
<?php
require_once(APPPATH.'/controllers/MyControllerBase.php');
class SomeController extends MyControllerBase {
...
}
?>
I hope this helps and of course, if you have better suggestions, please post them here.
Peter