Welcome Guest, Not a member yet? Register   Sign In
Super easy template system
#1

[eluser]Unknown[/eluser]
I just transferred from my personal framework to Codeigniter for a client's project and wasn't thrilled with all the confusing template plugins out there, so I took my template own template module from my personal framework and got it to work with CI.

Just thought I'd share.. it's way less confusing than any of them that I've seen. A template library doesn't need pages of documentation.

So here you go. A template library with only 30 lines of code.

Code:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
* CodeIgniter Template Library
*
* @author       Jordan Stout <[email protected]>
* @usage        1) Create file application/libraries/Template.php & insert class below.
*               2) Create a template controller (ie. "controllers/common/header.php")
*                  and give it a public variable $id (ie. "public $id = 'header'").
*                  Instead of setting the view by $this->load->view('common/header', $data),
*                  return the view data by doing. (ie. "return $this->load->view('common/header', $data, true)")
*               3) Set autoloader or, in your controller, call using $this->load->library('template').
*               4) Then all you have to do is include whatever view file you want by doing:
*                  $this->template->set('common/header', 'common/footer', ........)
*               5) Then inside your view, you call what you set the public $id to.
*                  (ie. "&lt;?php echo $header; ?&gt;" will give the template file!)
*/

class Template
{
    function set()
    {
        global $CI;
        
        foreach (func_get_args() as $template)
        {
            $file = APPPATH.'controllers/' . $template . EXT;
            if (!is_file($file))
            {
                show_error('Unable to load your child ' . $template);
            }
            
            include ($file);
            $template = explode('/', $template);
            $template = preg_replace('/[^a-zA-Z0-9]/', '', $template[count($template)-1]);
            
            $controller = new $template();
            
            if (isset($this->_ci_cached_vars[$controller->id]))
            {
                show_error('ID ' . $controller->id . '.  has already been set.');    
            }
            
            $data[$controller->id] = $controller->index();
        }
        
        $CI->load->vars($data);    
    }
}

Enjoy Smile

UPDATE:
Since CI can't instantiate multiple controllers, calling CI functions within the template files doesn't work. Work arounds for this include using the global $CI, then calling $CI->method(), or using the Base Class method (which is what I've done). If you have a workaround for this, please let me know!


Messages In This Thread
Super easy template system - by El Forum - 10-13-2010, 04:10 PM
Super easy template system - by El Forum - 10-13-2010, 08:47 PM
Super easy template system - by El Forum - 10-13-2010, 09:59 PM



Theme © iAndrew 2016 - Forum software by © MyBB