EllisLab text mark
Advanced Search
     
Template Engine / Easier View Library
Posted: 13 December 2007 07:44 PM   [ Ignore ]
Avatar
Joined: 2007-06-10
2919 posts
<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
 * Template Engine.
 * 
 * Has 2 modes of use:
 * 1: Using a container template with template parts (blocks) inside.
 * 2: Using template parts only (header,content,footer. etc).
 * 
 * Allows any data part to be re-assigned to all parts (global data).
 * Allows a template source directory to be specified.
 * Can be used with Matchbox modules.
 * 
 * In future: Output could render PDF files from HTML content.
 */
class Template 
{
     
var $file$data;
    
    
/**
     * Constructor
     *
     * @param    string    the file name of the template to load
     */
    
function Template($file null
    
{
        $this
->file $file;
        
$this->data = array
        (
            
'module'    => null,    // use with Matchbox modules
            
'directory' => null,
        );
    
}
    
    
/**
     * Assign data to this template object.
     * 
     * @param     string
     * @param     array or string
     */
    
function assign($key$value null
    
{
        
if (is_array($key)) 
        
{
            
foreach ($key as $k => $v
            
{
                $this
->data[$k] $v;
            
}
        }
        
else
        
{
            $this
->data[$key] $value;
        
}
    }
    
    
/**
     * Assign data to a specific template object.
     * 
     * @param    string
     * @param     string
     * @param     array or string
     */    
    
function assign_to($tpl$key$value null)
    
{
        $this
->data[$tpl]->assign($key$value);
    
}
    
    
/**
     * Assign data to all template objects.
     * 
     * @param     string
     * @param     array or string
     */
    
function assign_global($key$value null)
    
{
            
foreach ($this->data as $k => $v)     // iterate through data
            
{
                
if (get_class($v) == 'Template'// if data is template object
                
{
                    $v
->assign($key$value);     // assign to it
                
}
            }
            $this
->assign($key$value);         // assign to this template also
    
}
    
    
/**
     * Assign new template object parts to this template.
     * 
     * @param     string
     * @param     array or string
     */
       
function assign_tpl($tpl$file null)
       
{
        
if (is_array($tpl))
        
{
            
foreach ($tpl as $k => $v)
            

                $this
->assign($k, new Template($v));
            

        }
        
else
        
{
            $this
->assign($tpl, new Template($file));
        
}    
    }
  
    
/**
     * fetch a specific template data value.
     *
     * @param     string    the template name
     * @param    string    the data key
     * @return     string     
     */
      
function fetch($tpl$key)
      
{
            
return $this->data[$tpl]->data[$key];    
      
}
        
    
/**
     * Build the HTML output.
     *
     * @param     bool    set the output mode
     * @return     string     the rendered template
     */
    
function render($return FALSE
    
{        
        
foreach ($this->data as $k => $v)                // iterate through data
        
{
            
if (get_class($v) == 'Template')             // if data is template object
            
{
                $this
->data[$k] $v->out(TRUE);        // render it            

                
if (!$this->file) echo $this->data[$k]// output the part
            
}
        }
        
if ($this->file) return $this->out($return);
    
}
   
    
/**
     * render this template.
     *
     * @param     bool    set the output mode
     * @return     string     the rendered template
     */
    
function out($return FALSE)
    
{        
        $CI 
= & get_instance();
        return 
$CI->load->view($this->data['directory'].$this->file$this->data$return$this->data['module']);
    
}

UPDATED:
Added fetch() - template data.
Added assign_to() - assign data to a specific template

 Signature 

URI Language Identifier | Modular Extensions - HMVC | View Object | Widget plugin | Access Control library

 
Posted: 13 December 2007 07:51 PM   [ Ignore ]   [ # 1 ]   [ Rating: 0 ]
Avatar
Joined: 2007-06-10
2919 posts

Usage: Rendering page parts only

Autoload library template.php

$this->template->assign_tpl( array(
            
'header'       => 'header',      // name of the content part => file name to load
            
'header_write' => 'header_write',
            
'sidebar'      => 'sidebar_posts_form',
            
'editorjs'     => 'editorjs',
            
'posts_form'   => 'posts_form',
            
'footer'       => 'footer',
      ));
            
      
$this->template->assign_global( array(    // all the above parts have access to this data
            
'postid'          => $this->postID,
            
'users'           => $users,
            
'categories'      => $categories,
            
'writestyle'      => "active",
            
'writepostsstyle' => "active",
            
'pagetitle'       => "Administration Panel - Manage Posts",
            
'categoryids'     => $categoryids,
            
'directory'       => 'admin/',       // the template source directory
      
));
            
            
$this->template->render(); 
 Signature 

URI Language Identifier | Modular Extensions - HMVC | View Object | Widget plugin | Access Control library

 
Posted: 13 December 2007 08:15 PM   [ Ignore ]   [ # 2 ]   [ Rating: 0 ]
Avatar
Joined: 2007-06-10
2919 posts

Usage: Rendering page parts within container template.

Autoload library template.php

$base_nav = new Template('nav');      // navigation template part
        
$base_nav->assign('some'$data);
        
        
$content = new Template('content');   // content template part
        
$content->assign('some'$data);

        
$this->template->file 'main_view';  // container template
        
$this->template->assign(array(
            
'base_nav' => $base_nav,
            
'content' => $content,
        ));
                    
        
$this->template->render(); 

With the template library autoloaded page parts can be assigned to the template object from within controllers or models.

 Signature 

URI Language Identifier | Modular Extensions - HMVC | View Object | Widget plugin | Access Control library

 
Posted: 13 December 2007 09:15 PM   [ Ignore ]   [ # 3 ]   [ Rating: 0 ]
Avatar
Joined: 2007-06-10
2919 posts

Usage: Rendering different page parts in different Controller methods.

Autoload library template.php

function index()
      
{
        $indexView 
= new Template('index');
        
$indexView->assign( array(
        
"blogtitle"       => $this->blogTitle,
        
"blogdescription" => $this->blogDescription,
        
"categories"      => $this->categories,
        
"pages"           => $this->pages,
        
"blogroll"        => $this->blogroll,
        
"posts"           => $this->posts,
        
"page"            => $this->page,
        
"numpages"        => $this->mdl_posts->numpages,
        
"prevpage"        => $this->prevpage,
        
"nextpage"        => $this->nextpage,
        
"nav"             => $this->nav
        
));

        
$this->_final_out($indexView);
      
}
    

      
function _final_out($contentView
      
{
         $this
->template->assign_tpl( array(
         
'header'         => 'header',
         
'sidebar'        => 'sidebar',
         
'content'        => 'content',
         
'footer'         => 'footer',
         ));        

         
$this->template->assign('content'$contentView);    // re-assign the content part
         
$this->template->assign_global($contentView->data); // assign as global data 
         
$this->template->assign_global('directory'$directoryName); // add template directory

         
$this->template->render();
       
 Signature 

URI Language Identifier | Modular Extensions - HMVC | View Object | Widget plugin | Access Control library

 
Posted: 14 December 2007 03:07 AM   [ Ignore ]   [ # 4 ]   [ Rating: 0 ]
Avatar
Joined: 2007-06-10
2919 posts

Updated: See first post.
Added fetch()
Added assign_to()

$title $this->template->fetch('content','page_title');
    
$title .= 'Website name';
    
$this->template->assign_to('header''site_title'$title); 
 Signature 

URI Language Identifier | Modular Extensions - HMVC | View Object | Widget plugin | Access Control library

 
Posted: 14 December 2007 11:08 AM   [ Ignore ]   [ # 5 ]   [ Rating: 0 ]
Joined: 2006-11-11
59 posts

Thank you for your contribution.
Please also add a dedicated page in the wiki and provide some documentation.
CI story shows how a clean documentation can lead to success wink

 
Posted: 14 December 2007 02:15 PM   [ Ignore ]   [ # 6 ]   [ Rating: 0 ]
Avatar
Joined: 2006-08-06
918 posts

thanks for the contribution!

as well as adding some short instructions for usage, could you add some information on how this approach is different from and/or the same as and/or better than other view libraries (and related solutions) detailed in the FAQ section on view libraries?

that would really help differentiate your approach. i’ve added this forum post to the FAQ already.

 Signature 

peeker email (imap/pop) | site_migrate | OOCalendar | PhotoBox2 | word_limiter