Welcome Guest, Not a member yet? Register   Sign In
Template Library
#1

[eluser]kcmerrill[/eluser]
I realize this is probably a topic that has been discussed Ad Nauseam, however, in the spirit of open source and to try to give back to the community, here is a very basic template library code contribution that a co-worker and I came up with.

The idea behind the library is to enable multi theming support, allow the inclusions of headers/footers/sidebar and an area for content without explicitly calling each view in each controller function. Of course, feel free to hack it as you see fit. I'll go over the pretty basic installation steps, and a few examples of it's usage.

1.) Within your application/config/autoload.php, add "template" to your library, I do this simply because I'll be using the template viewing system everywhere.

2.) Within your application/config/config.php add a config variable named theme. If this is not set, the default theme will be "default". The theme, is essentially a location to look at to get your view files, so for example, if you had a blue theme, and your config value was set to "blue", it would look at application/views/blue/some_view_here.php.

Also, as a side note, all you need to do is simply reset your config setting via $this->config and you can change themes on the fly, assuming you have multiple themes setup.

2.) Copy this library class to system/libraries/template.php
Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/*
| -------------------------------------------------------------------
| TEMPLATE LIB(Home Grown)
| -------------------------------------------------------------------
| This templating system will be used to control the theme and available
| files within the application
| -------------------------------------------------------------------
| EXPLANATION OF VARIABLES
| -------------------------------------------------------------------
|
|    ['displayItems'] An array of key=>values used globally to set variables
|
*/

class template {
    var $displayItems;
    #Set variables to be used in the view
    function set($key,$value){
        if(!empty($key))
            $this->displayItems[$key] = $value;
    }

    function get($key){
        return $this->displayItems[$key];
    }

    function showVariables(){
        foreach($this->displayItems as $k=>$v){
            if(!is_array($v))
                echo "<b>\$$k</b>: $v<br>\n";
            else{
                foreach($v as $subK=>$subV){
                    echo "<b>\${$k}['$subK']</b>: $subV<br>\n";
                }
            }

        }
    }

    function view($viewFile,$return=false){
        #Create an instance
        $CI = & get_instance();
        $theme = $CI->config->item("theme");

        #set the default theme if no config was set
        if(!$theme)
            $theme = "default";

        #Setup some basic theme variables
        $this->set("siteUrl",site_url());
        $this->set("themeUrl",site_url()."system/application/views/".$theme."/");
        $this->set("themeDirUrl",site_url()."system/application/views/");
        $this->set("theme",$theme);

        #We can create as many of these as we want, but for now we will use these 4
        $this->_header = $CI->load->view($theme."/header",$this->displayItems,TRUE)."\n&lt;!-- Ending {$viewFile} header call --&gt;\n";
        $this->_leftbar = $CI->load->view($theme."/leftbar",$this->displayItems,TRUE)."\n&lt;!-- Ending {$viewFile} leftbar call --&gt;\n";
        $this->_footer = $CI->load->view($theme."/footer",$this->displayItems,TRUE)."\n&lt;!-- Ending {$viewFile} footer call --&gt;\n";

        #This is what was originally called. The content.
        $this->_content = $CI->load->view($theme."/".$viewFile,$this->displayItems,TRUE)."\n&lt;!-- Ending {$viewFile} content call --&gt;\n";

        #Lets set these up as variables to our index file.
        $view["header"] = (isset($this->_header) ? $this->_header : "");
        $view["leftbar"] = (isset($this->_leftbar) ? $this->_leftbar : "");
        $view["footer"] = (isset($this->_footer) ? $this->_footer : "");
        $view["content"] = (isset($this->_content) ? $this->_content : "");

        return $CI->load->view($theme."/index",$view,$return);
    }
}
?&gt;

3.) Within your application/view folder, you should have at least a default theme, however, you can change this to whatever you'd like. For this example, I'll be using default as the default theme.

Create the folder: application/view/default
Next, create an index.php file which should look something like this:
Code:
&lt;?=$header?&gt;
&lt;?=$leftbar?&gt;
&lt;?=$content?&gt;
&lt;?=$footer?&gt;

Keep in mind, you can change the order you want, add extra items here, ect .... but first a quick explination.
application/view/default/index.php will contain the code above.
application/view/default/header.php will contain the header view
application/view/default/leftbar.php will contain the left navigation view, rinse and repeat.

$content will contain the file originally called in the view, so a quick example.
Code:
$this->template->view('welcome');

This will take the header view, the left bar view, the welcome view and then the footer view and build it within that order.

If within your welcome view for example, you do not wish to have the header/footer etc within the template for various reasons, you can simply unset the header/footer etc ....
Code:
unset($this->template->_header);

4.) Setting and retrieving the view variables is pretty simplistic.
Code:
$this->template->set("email","[email protected]");

The variable $email will now be able to used within each of the views, be it the header/footer/leftbar/ or within your content view.

Hope this helps,
-kc




Theme © iAndrew 2016 - Forum software by © MyBB