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!
#2

[eluser]Rolly1971[/eluser]
i don't mean to be critical but there is a better way. By what i see each view partial needs it's own controller. Instead of that method:

to load common items create a MY_Controller class (and for CI1.7.2 save it under application/libraries. It will be auto magically loaded by CI, CI 2.0 save it under application/core) that extends Controller, then extend all your controllers from it instead. Use this Controller to load all your common items such as : header, footer, template. Then your controllers in application/controllers load the content required and then you can either send the content from your controllers to MY_Controller for merging into the template and perform the output or store the built template that MY_Controller creates inside a variable that the page controller can access and merge them in there.

The way i do it is like this sorta. I have a MY_Controller that loads all common view partials, the page controllers extend it, they also request data from the db through a class i made called: theme_model. This class loads content from the database. once the controller has all it's info it passes that to the MY_Controller which puts the final page to the browser.

CI comes with a template parser class. fairly basic in its implimentation, but also very easy to use and wrap a template library around.

check out Phil Stergeons template library. Or Colin Williams Template Library, i think they may work quite well for you they are very simplistic in their elegance.

If you prefer to do it yourself like me, they will at least give you some insite into creating a template class in CI.
#3

[eluser]Unknown[/eluser]
I agree to an extent. Using a generic template library works in some cases... but I need control over everything that gets loaded, so I've done it like this for a while. I used a system like the one I posted above for internal software systems, but I've also used some like what you suggested in the past and they work well for something who's header / footer are static. The way I listed, you can load "modules" into the file and those modules can be their own controller. I didn't even test what I posted above out and it wasn't really working while calling multiple Controllers, so I extended Controller and I'm almost to what I need.




Theme © iAndrew 2016 - Forum software by © MyBB