PHP Classes
Icontem

File: ctl.tpl.class.php


  Search   All class groups All class groups   Latest entries Latest entries   Top 10 charts Top 10 charts   Newsletter Newsletter   Blog Blog   Forums Forums   Help FAQ Help FAQ  
  Login   Register  
Recommend this page to a friend! ReTweet ReTweet Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Emilis Dambauskas  >  ctlTpl  >  ctl.tpl.class.php  
File: ctl.tpl.class.php
Role: Class source
Content type: text/plain
Description: Class source
Class: ctlTpl
Very simple but very powerful template class
 

Contents

Class file image Download
<?php

/** 
 * ctlTpl template engine.
 * 
 * @package tv2-engine
 * @author Emilis Dambauskas (emilis@gildija.lt)
 * @copyright 2002–2003 Emilis Dambauskas under {@link http://opensource.org/licenses/artistic-license.php Artistic license}
 * @version $Id: ctlTpl.class.php,v 1.8 2004/05/13 08:21:52 lunaticlt Exp $
 * @class ctlTpl 
 *
 * @log 2003-07-07 Full code comments
 * @log 2003-07-21 Added method loadLibrary()
 * @log 2003-07-24 Added checking if templates exist by Linas Gricius linas(a)gricius.net
 * @log 2003-08-04 Added method checkVars
 * @log 2003-08-04 Fixed bug with quieting down fatal errors in methods process() and template().
 * @log 2003-08-04 Fixed constructor comments
 * @log 2004-05-13 Replaced variable extraction loops with extract(). Thanks Andrius Juozapaitis for noticing.
 */
class ctlTpl
{
    
/**
     * Stores default temlpate directory name
     * @attribute private string tpl_dir
     */
    
var $tpl_dir;
    
    
/**
     * Stores PHP error reporting level while parsing
     * @attribute private int e
     */
    
var $e;
    
    
/**
     * Template variable storage
     * @attribute private array vars
     */
    
var $vars;
    
    
/**
     * Template filename
     * @attribute private string file
     */
    
var $file;
    
    
/**
     * Temporary variable used while parsing (stores var names).
     * @attribute private string v
     */
    
var $v;
    
    
/**
     * Temporary variable used while parsing (stores var values).
     * @attribute private mixed k
     */
    
var $k;
    
    
    
/**
     * Variable used to enable/disable checking if template exists or not.
     * @attribute private boolean cie
     */
    
var $cie;


    
/**
     * Constructor. Sets default template directory and clears vars array.
     *
     * @constructor ctlTpl  
     * @param optional string $td default template directory (default value './' -- current directory)
     * @param optional boolean $cie enable checking if template exists or not
     */  
    
function ctlTpl($td './'$cie FALSE)
    {
        
$this->tpl_dir $td;
        
$this->vars = array();
        
$this->cie $cie;
    }
    

    
/**
     * Creates/changes template var value
     *
     * @method public setVar  
     * @param string $name variable name 
     * @param mixed $value variable value  
     */  
    
function setVar($name$value)
    {
        
$this->vars[$name] = $value;
    }
    

    
/**
     * Creates/changes multiple template variables
     *
     * @method public setVars  
     * @param array $array variable array. Format should be: array('varname1'=>'value1', 'varname2'=>'value2', ...).
     */  
    
function setVars($array)
    {
        
$this->vars array_merge($this->vars$array);
    }

    
    
/**
     * Registers a reference to an external variable as a tpl var:
     *
     * @method public regVar  
     * @param string $name variable name 
     * @param ref mixed $var variable reference  
     */  
    
function regVar($name, &$var)
    {
        
$this->vars[$name] = &$var;
    }
    

    
/**
     * Clears a variable
     *
     * @method public unsetVar  
     * @param string $name variable name  
     */  
    
function unsetVar($name)
    {
        unset(
$this->vars[$name]);
    }
    

    
/**
     * Clears all stored template vars
     *
     * @method public clearVars   
     */  
    
function clearVars()
    {
        
$this->vars = array();
    }
    
/**
     * Alias of {@link ctlTpl::clearVars}.
     * 
     * @method public unsetAllVars
     */
    
function unsetAllVars()
    {
        return 
$this->clearVars();
    }

    
    
/**
     * Loads a function library
     *
     * @method public loadLibrary
     * @param string $filename function library name  
     * @since 2003-07-21
     */
    
function loadLibrary($filename)
    {
        if (
file_exists($filename) && is_file($filename))
            require_once(
$filename);
        else if (
file_exists($this->tpl_dir.$filename) && is_file($this->tpl_dir.$filename))
            require_once(
$this->tpl_dir.$filename);
    }

    
    
/**
     * Checks if template exists or not. 
     *
     * @method private checkTpl  
     * @param string $filename function library name  
     */
    
function checkTpl($filename)
    {
        if (
file_exists($filename) && is_file($filename))
            return 
true;
        else
            die(
'<p style="color: #CC0000"><strong>TPL error: '.$filename.'</strong> does not exist!<p>');
    }

    
    
/**
     * Checks if template var[s] exist
     *
     * @method public checkVars
     * @return boolean TRUE if all vars exist, FALSE otherwise 
     * @param mixed $var_names variable name array or variable name string  
     */  
    
function checkVars($var_names)
    {
        
$var_names = (array) $var_names;
        foreach (
$var_names as $vn)
            if (!isset(
$this->vars[$vn]))
                return 
FALSE;
    
        return 
TRUE;
    }


    
/**
     * Parses the given template file and clears vars if needed.
     *
     * @method public process
     * @return string output from template 
     * @param string $file template file name (relative to default template dir or absolute).
     * @param optional boolean $clr_vars should the variables be cleared after parsing? Default: FALSE.
     */  
    
function process($file$clr_vars FALSE)
    {
        
// check if filename is absolute or relative
        // (both for absolute unix and windows paths):
        
if ($file{0} == '.' || $file{0} == '/' || $file{1} == ':' || $file{0} == '\\')
                
$this->file $file;
        else
                
$this->file $this->tpl_dir.$file;
        
        unset(
$file);
        
        
// If enabled...
        
if ($this->cie)
            
$this->checkTpl($this->file);
                    
        
// create vars from data array
        
extract($this->varsEXTR_OVERWRITE);
        
        
$this->error_reporting(E_ALL E_WARNING E_NOTICE); // quiet down warnings and notices:
        
ob_start(); // start output buffering:
        
require $this->file// parse the file. Produces fatal error if file not found.
        
error_reporting($this->e); // restore previous error reporting level
        
$r ob_get_contents(); // get parsed text
        
ob_end_clean(); // quietly end output buffering
        
        // clear vars:
        
if ($clr_vars)
                
$this->vars = array();
        
        return 
$r// uh-oh what does this do? ;)
    
}
    
    
/**
     * Includes the result from the template script that is 
     * being parsed now into template specified by $name.
     * The result is stored in a variable specified by $myname.
     * NOTICE: this method should be called AT THE END of any template script.
     *
     * @method public template
     * @return string output from template 
     * @param string file template file name (relative to default template dir or absolute). 
     * @param optional string myname variable name for included text.
     * @param optional boolean clr_vars should the variables be cleared after parsing? Default: FALSE.  
     */
    
function template($file$myname 'include'$clr_vars FALSE)
    {
        
// check if filename is absolute or relative
        // (both for absolute unix and windows paths):
        
if ($file{0} == '.' || $file{0} == '/' || $file{1} == ':' || $file{0} == '\\')
                
$this->file $file;
        else
                
$this->file $this->tpl_dir.$file;
        unset(
$file);
        
        
// create vars from data array
        
extract($this->varsEXTR_OVERWRITE);
        
        
error_reporting($this->e); // restore previous error reporting level
        
$$myname ob_get_contents(); // get parsed text
        
ob_end_clean(); // quietly end output buffering
        
        
$this->error_reporting(E_ALL E_WARNING E_NOTICE); // quiet down warnings and notices:
        
ob_start(); // start output buffering:
        
require $this->file// parse the file. Produces fatal error if file not found.
    
}
    
    
}

?>

 
  Advertise on this site Advertise on this site   Site map Site map   Statistics Statistics   Site tips Site tips   Privacy policy Privacy policy   Contact Contact  

For more information send a message to :
info at phpclasses dot org.
Copyright (c) Icontem 1999-2009 PHP Classes - PHP Class Scripts
  PHP Book Reviews - Reviews of books and other products