• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
A simple template system

#1
[eluser]jmb727[/eluser]
This is a very basic template system that allows you to group sets of view files in your application and change between these sets as you wish, creating templates, sort of.

Add this to your application/config/config.php file.

Code:
$config['active_template'] = 'default';

Next, we're gunna tinker the CodeIgniter Loader class.

Open system/core/Loader.php and place the following code just after the end of first else code block in the _ci_load() function (around line 607 in CodeIgniter 2.0.2 maybe earlier versions to).

Code:
$CI =& get_instance();
$_active_tpl = $CI->config->item('active_template');
if ($_active_tpl != FALSE) {
    $tmp_ci_path = str_replace('views/', 'views/'.$_active_tpl.'/', $_ci_path);
    if (file_exists($tmp_ci_path)) {
        $_ci_path = $tmp_ci_path;
    }
}

This code will get your 'active_template' config item and try to load whatever view file you choose with $this->load->view() from 'views/default/'. It checks to see whether the file exists first, if not it defaults to the views folder.

E.g.

Code:
$this->load->view('index');

If index.php exists in application/views/default/ then that file is loaded, if not it will attempt to load application/views/index.php.

I know you could just as easily type $this->load->view('default/index') but my method gives you more flexibility.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.