Location: PHPKode > scripts > 3SS Template Engine > 3ss-template-engine/threesste.class.php
<?php
class threessTE 
{
    ///////////////////////////////////////////////
    //
    //    3 Service Solution
    //    http://www.3servicesolution.com/
    //    Author: Brant Messenger
    //
    //    COPYRIGHT:
    //    http://creativecommons.org/licenses/by-sa/2.0/
    //
    //    LOCATION:
    //    http://www.3servicesolution.com/_classes/threesste.class.inc
    //
    //    VERSION: 1.2.5
    //
    ///////////////////////////////////////////////

	var $template; // STRING
    var $shrink; // BOOL
    var $links; // ASSOC ARRAY
    var $string = ''; // STRING for building 
    var $data = Array(); // ARRAY of Data (Variables)    
    
    // INITIALIZE
    function threessTE($vFile) 
	{
        $this->template = $vFile;
    }

    // SET ALL DEFINED CONSTANTS TO THE DATA ARRAY
	function setConstants($aVars='')
    {
    	$aDefined = get_defined_constants();
        
        if (isset($aVars) && is_array($aVars))
        {
            $aDefined = array_merge($aDefined, $aVars);
        }
        
        if (count($aDefined) > 0)
        {
            foreach($aDefined as $vTempKey => $vTempValue)
            {
            	$this->setData($vTempKey,$vTempValue);
            }        
        } 
        else $this->setError(__FUNCTION__.' could not be initialized.');   
    }
	
    // OUTPUT ALL THE VARS AND TEMPLATE INFORMATION
	function debug() 
	{
		echo '
		<div style="text-align:left;background-color:#eee;padding:10px;border-top:2px solid #c30">
			<b>'.__CLASS__.' CLASS:</b> [<blockquote><pre>'.print_r(get_object_vars($this),true).'</pre></blockquote>]
		</div>';
	}
	
    // SET ERROR(s) AND ATTACH TO {{ERROR}} VAR
	function setError($vString) 
	{
		$this->error[] = $vString;

		$error = implode("<br />\n\t",$this->error);
		
		$this->setData('ERROR',"<div class=\"error\">\n\t$error\n</div>\n");
	}
	
    // SET TEMPLATE DATA
	function setData($vName,$vValue) 
	{
		if (is_array($vValue))
		{
			foreach($vValue as $vTempKey => $vTempValue)
			{
				$this->data[$vName][$vTempKey] = $vTempValue;
			}
		}
		else
		{
			$this->data[$vName] = $vValue;
		}
	}
    
    // SET LINK
	function setLink($vName,$vValue) 
	{
        $this->links[$vName] = $vValue;
	}     
	
    // RETURN A VAR's VALUE THAT HAS ALREADY BEEN SET
	function returnData($vName)
	{
		return $this->data[$vName];
	}

    // RETURN A STRING OF ANY FILE
	function getFile($vFile)
	{
		if (function_exists('file_get_contents'))
    	{
      		$vString = @file_get_contents ($vFile);
			
		}
		else
		{
			$vString = implode('', @file($vFile));
		}
		
		if (!$vString)
		{
			echo "[$vFile] does not exist.";
			exit;
		}
		else return $vString;
	}
    
    // INSERT COPYRIGHT INTO
	function insertCopyright($vString)
	{
		$vSearch = '</title>';
		$vReplace = '</title>
    <meta name="author" content="Brant Messenger @ http://www.3servicesolution.com/" />
    <meta name="generator" content="threessTE @ http://www.3servicesolution.com/_classes/threesste.class.inc" />';

		return str_replace($vSearch,$vReplace,$vString);
	}
	
    // SHRINK
    function shrinkOutput($vString)
    {
        $vString = str_replace(array("\n","\r","\t"),"",$vString);
            
        // CHECK DOUBLE SPACES
        while(strstr($vString, '  ') > -1)
        {
            $vString = str_replace('  ',' ',$vString);
        }
    
        return trim($vString);
    }
            
    // SEARCH AND REPLACE TEXT WITH LINK
    function setTextToLinks($vString)
    {
        if (is_array($this->links) && count($this->links) > 0)
        {        
        
            foreach($this->links as $vText => $vUrl)
            {
                $vPattern = '/\b';
                $vPattern .= '(?<!<a |=")';
                $vPattern .= '('.$vText.'(\'s|s|ies)?)';
                $vPattern .= '(?!\.[\w]{3,4}|[^<]*<\/[aA]{1}>)';
                $vPattern .= '\b/i';                             
                $vReplacement = "<a href=\"$vUrl\">\\1</a>";
                
                $vString = preg_replace($vPattern,$vReplacement,$vString);
            }
            
            return $vString;
        }
        else return $vString;
    }
    
    // FINAL END OF OBJECT BY RETURNING THE OUTPUT OF VARS AND TEMPLATES
	function returnOutput ()
	{
		$vString = $this->getFile($this->template);
        
        if (is_array($this->data))
		{
			// Set ERROR var to the proper value
			if (!isset($this->data['ERROR'])) $this->data['ERROR'] = NULL;
			
			// Replace vars in Template
            foreach ($this->data as $vTempKey => $vTempValue) {
            	$vString = str_replace('{{'.$vTempKey.'}}',$vTempValue,$vString);
            }
			
            // CHECK TEXT FOR LINKS
            $vString = $this->setTextToLinks($vString);

			// Replace vars in setData
            foreach ($this->data as $vTempKey => $vTempValue) {
            	$vString = str_replace('{{'.$vTempKey.'}}',$vTempValue,$vString);
            }
		}
        
        // INSERT COPYRIGHT
        $vString = $this->insertCopyright($vString);

        // SHRINK FILE SIZE
        if ($this->shrink) $vString = $this->shrinkOutput($vString);
        
        return $vString;
    }
    
    // OUTPUT PARSED TEMPLATE TO A FILE
	function writeOutput($vFile)
	{
		$vString = $this->returnOutput();

		if (!$vHandle = fopen($vFile, 'w'))
		{
			$this->setError("[".$vFile."] can not be opened.");
    	}
		
		if (fwrite($vHandle, $vString) === FALSE)
		{
			$this->setError("[".$vFile."] can not be written to.");
        }
		
		fclose($vHandle);

	}
    
    function setString($vString)
    {
        if ($this->string != '') $this->string .= $vString;
        else $this->string = $vString;
    }
    
    function clearString()
    {
        $this->string = '';
    }
}
?>
Return current item: 3SS Template Engine