<?php
////////////////////////////////////////////////////////
// class of tpl
////////////////////////////////////////////////////////
// This temp is just copied from EasyTemplate 
////////////////////////////////////////////////////////

class tpl 
{
	// the error message

	var $error = "";    
	
	var $tags = array();            // The tag values pairs, as defined by assign()
    var $tpl_file = "";        

	function tpl ( $tplname )
	{
		if ( !file_exists( $tplname ))
		{
			$this->error = 'The file '.$tplname.' is not exists ....';
		}
		
		$this->tpl_file = $tplname ;

	}

	function assign ( $tag , $value )
	{
		// If the supplied tag name is empty, bail out
        if(empty($tag))
        {
            $this->error = "Tag name is empty";
            return(false);
        }
        
        // Assign value to tag
        $this->tags[$tag] = $value;
        
        return(true);
	}

	function parse()
    {
        // Read in template file, suppress error messages
        $contents = @implode("", (@file($this->tpl_file)));
        
        // Loop through all assigned tag-value pairs
        while(list($key, $value) = each($this->tags))
        {
            // Construct the template tag name 
            $tag = '{'.$key.'}';
            
            // Is there such a tag in the template?
            if(!strstr($contents, $tag))
            {
                $this->error = "Tag $tag not found in template ".$this->tpl_file.".";
                return(false);
            }
            
            // Replace the template tag with the respective value
            $contents = str_replace($tag, $value, $contents);
        }
    
        // Return the parsed template    
        return($contents);    
    }
    
    /*
     *      bool easy_print()
     *      Parse and print the current template
     *      Sets $this->error and returns false on error, returns true on success
     */
    function printe()
    {
        // Parse the template
        $ret = $this->parse();
        
        // Error found?
        if($ret == false)
        {
            return(false);
        }

        // Output the parsed template       
        echo $ret;
        
        return(true);
    }



}


?>