Welcome Guest, Not a member yet? Register   Sign In
rudimentary template page chunks in my views, comments welcome
#1

[eluser]Flying Fish[/eluser]
I'm at the point when I need to start including common chunks of html in my views, like a header, footer, etc.

My goals were
be able to clearly include common page chunks in my view files
make changes in one master template file
avoid using something complex from the wiki that I can't understand :-)

Here's my possibly laughable attempt, I created a custom library called 'Template' and put it in
Code:
/system/application/libraries/Template.php

Here's what the file looks like
Code:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

/*
    The Template Class provides simple templating functionality
    
    You will see this code at the beginning of each function
    
    $CI =& get_instance();
    
    It assigns a variable to this instance of the codeigniter object.
    This allows me to use native codeigniter classes like session userdata in this custom class

*/

class Template
{
    
    function __construct()
    {
    
    }
    
    function show($piece)
    {        
        $CI =& get_instance();
        
        $pieces = array(
            'html_head'   => $CI->load->view('_inc/head', '', TRUE),
            'masthead'    => $CI->load->view('_inc/masthead', '', TRUE)
        );
        
        foreach ($pieces as $key => $value):
            
            if($piece === $key)
            {
                return $value;
            }
        
        endforeach;

    }
    
}


/*
    End of file Template.php
    Location: /system/application/libraries/Template.php
*/

I can auto load this library, and when I need a page chunk I just call something like this in my view
[code]<?=$this->template->show('head')?>


your thoughts and comments are welcome


[/code]


Messages In This Thread
rudimentary template page chunks in my views, comments welcome - by El Forum - 03-30-2009, 03:03 PM



Theme © iAndrew 2016 - Forum software by © MyBB