Welcome Guest, Not a member yet? Register   Sign In
Simple template library
#1

[eluser]garrettheel[/eluser]
This is a very simple template library for those who want to add a global header and footer to their page as well as having the flexibility to assign variables to the views. Any number of views can also be loaded in the middle of the header and footer. This is the most basic version and I'll update it as I do so in my application.

Template library 0.1

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

class Template {
    
    public $CI;
    
    // Directory that the template resides in, appended to views/ (e.g views/default/).
    public $dir = 'default/';
    
    // Views to be loaded on page.
    public $views = array();
    
    public $header = array(
            "title" => "Default Page Title"
        );
    
    public $footer = array();
    
    public function __construct()
    {
        $this->CI =& get_instance();
    }
    
    public function add_view($view, $data = NULL)
    {
        $page = array($view => $data);
        
        $this->views = array_merge($this->views, $page);
    }
    
    public function render()
    {
        $this->CI->load->view($this->dir . "header", $this->header);
        
        foreach ($this->views as $view => $data):
            $this->CI->load->view($view, $data);
        endforeach;
        
        $this->CI->load->view($this->dir . "footer", $this->footer);
    }
}

Example usage:

Code:
$this->load->library('template');

// Override the default title
$this->template->header['title'] = "My fancy new title";

$this->template->add_view('someview', $somedata);
$this->template->add_view('anotherview', $moredata);
$this->template->render();

I've only left in the 'title' parameter to the header data because this was the only one that pretty much everyone would need. More can easily be added and assigned. Also, it is assumed that the template files are located in /views/default. You can change the location and even make different templates and change which one is used with $this->template->dir = "newtemplate/"

I'm sort of new to CI, so some feedback would be helpful.
#2

[eluser]xwero[/eluser]
I wonder why you limited the library to a header and a footer. This means the header has to contain the start html tag + the head tag + start body tag and the footer has to contain the end of the body and html tag. What if you want to reuse the header you have to make make a new file because the head content will be different.

I think it's a good exercise for yourself but i'm not sure it's an improvement on CI's view functionality. Your example can be written as
Code:
$this->load->view('default/header',array('title'=>"My fancy new title"));
$this->load->view('someview',$somedata);
$this->load->view('anotherview', $moredata);
$this->load->view('default/footer');
If the library gets autoloaded you only lose one line.

I think if you get annoyed by adding the same parts over and over you better use a base template and add content to it.
#3

[eluser]garrettheel[/eluser]
It's not meant to be a groundbreaking library, just something simple for not repeating the header and footer. And if you want to do different things with the header, it wouldn't be hard to change the library. I just hate calling the header and footer in EVERY single controller function and I think this way gives me more flexibility. Plus, if you have other page elements you require (like a dynamic sidebar) that would mean another extra live saved for every function.
#4

[eluser]xwero[/eluser]
I think a base template offers a better solution to do this. And for a more container layout you can choose between creating a more container template and adding a partial to an existing container.
#5

[eluser]textnotspeech[/eluser]
I think this is great. Simple and easily extendable. On that note I would only change one thing. I would add the add_view() parameters to render() so, if I'm only calling one view in my header-footer sandwich (which is typical for me) I can do that with just a call to render instead of add_view(), then render(). This would let you use it both ways:
Code:
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Template {
    
    public $CI;
    
    // Directory that the template resides in, appended to views/ (e.g views/default/).
    public $dir = 'default/';
    
    // Views to be loaded on page.
    public $views = array();
    
    public $header = array(
            "title" => "Default Page Title"
        );
    
    public $footer = array();
    
    public function __construct()
    {
        $this->CI =& get_instance();
    }
    
    public function add_view($view, $data = NULL)
    {
        $page = array($view => $data);
        
        $this->views = array_merge($this->views, $page);
    }
    
    public function render($view = FALSE, $data = NULL)
    {
        if($view)
        {
            $this->add_view($view, $data);
        }
        
        $this->CI->load->view($this->dir . "header", $this->header);
        
        foreach ($this->views as $view => $data):
            $this->CI->load->view($view, $data);
        endforeach;
        
        $this->CI->load->view($this->dir . "footer", $this->footer);
    }
}

then the usage could be one call like so:
Code:
$this->template->render('my_view', $data);
or:
Code:
$this->template->add_view('some_view', $some_view_data);
$this->template->add_view('another_view', $another_view_data);
$this->template->render();
#6

[eluser]garrettheel[/eluser]
Thanks for the positive feedback. I actually thought of the exact same thing not long after I posted this and had added it to the library I was using but hadn't updated it on here yet. That way one line can be used to create the entire default template with just one view and set of data Smile




Theme © iAndrew 2016 - Forum software by © MyBB