<?php
/**
* Template Tag Parser,  works good with HTMLTag
*@author bibby <bibby@surfmerchants.com>
*$Id$
*/

define('TPL_STRING','_tpl_string_');

class 
Tpl
{
    var 
$file;
    var 
$attribs=array();
    var 
$buffer;
    var 
$loop=false;
    var 
$loopData=array();
    var 
$output;
    var 
$clearFlag=false;
    
    function 
Tpl($file,$attribs=false)
    {
        
$this->file $file;
        
$this->setAttribs($attribs);
        
$this->loadTpl();
    }
    
    
/*** parse ***
    @access public
    @return string
    */
    
function parse()
    {
        
$b $this->buffer;
        
$this->output='';
        if(
$this->loop && is_array($this->loopData))
        {
            foreach(
$this->loopData as $set)
            {
                
$this->clearAttribs();
                
$this->setAttribs($set);
                
$this->output .= $this->merge($b);
            }
        }
        else
        {
            
$this->output $this->merge($b);
        }
        
        return 
$this->output;
    }
    
    
/*** loadTpl ***
    @access private
    @return bool success
    */
    
function loadTpl()
    {
        if(
$this->file == TPL_STRING)
            return 
true;
        
        
$f = @file_get_contents($this->file);
        if(!
$f)
            die(
"cannot locate tpl file $this->file");
        
        
$this->setBuffer($f);
        return 
true;
    }
    
    
/*** setBuffer ***
    @access    public
    @param    string
    @return    void
    */
    
function setBuffer($str)
    {
        
$this->buffer $str;
    }
    
    
/*** setAttrib ***
    @access set a tpl replace pair
    @param string tag in the template
    @param string text to replace with
    @return void
    */
    
function setAttrib($k,$v)
    {
        
$this->attribs[$k]=$v;
    }
    
    
/*** getAttrib ***
    @access public
    @param string tpl tag
    @return string stored value
    */
    
function getAttrib($k)
    {
        return 
$this->attribs[$k];
    }
    
    
/*** clearAttribs ***
    @access    public
    @return void
    */
    
function clearAttribs()
    {
        
$this->attribs=array();
    }
    
    
/*** setClear ***
    @access    public
    @param bool    on/off
    @return    void
    */
    
function setClear($on=true)
    {
        
$this->clearFlag = !!$on;
    }
    
    
/*** loop ***
    @access public
    @param array where one would setAttribs an array, loopData is an array of *those*
    @return bool success
    */
    
function loop($loopData)
    {
        if(!
is_array($loopData))
            return 
false;
            
        
$this->loop=true;
        
$this->loopData array_merge($this->loopData,$loopData);
        return 
true;
    }
    
    
/*** merge ***
    @access private
    @param string tpl buffer
    @return string html
    */
    
function merge($buffer)
    {
        foreach(
$this->attribs as $k=>$v)
        {
            if(
is_object($v) && method_exists($v,'parse'))
                
$V $v->parse();
            else
                
$V=$v;
            
$buffer str_replace('{'.strtoupper($k).'}',$V,$buffer);
        }
        
        if(
$this->clearFlag)
            
$buffer preg_replace("/{.*}/","",$buffer);
        
        return 
$buffer;
    }
    
    
/*** string ***
    static method to create a Tpl from string
    @access    public    static
    @param    string    Tpl buffer
    @return    object    Tpl
    */
    
function string($str$attribs=false)
    {
        
$T = new Tpl(TPL_STRING$attribs);
        if(
is_a($str,'HTMLTag'))
            
$str $str->parse();
        
$T->setBuffer($str);
        return 
$T;
    }
}
?>