/**
* Parses template file
* @param template filename
* @return parsed template
*/
function parseTemplateFile($templateFile)
{
$searchPattern = "/\{([a-zA-Z0-9_]+)\}/i"; // macro delimiter "{" and "}"
$replacementFunction = array(&$this, 'parseMatchedText'); //Method callbacks are performed this way
$fileData = file_get_contents($templateFile);
$parsedTemplate = preg_replace_callback($searchPattern, $replacementFunction, $fileData);
return $parsedTemplate;
}
/**
* Parses template data
* @param template data
* @return parsed data
*/
function parseTemplateData($templateData)
{
$searchPattern = "/\{([a-zA-Z0-9_]+)\}/i"; //macro delimiter "{" and "}"
$replacementFunction = array(&$this, 'parseMatchedText'); //Method callbacks are performed this way
$parsedTemplate = preg_replace_callback($searchPattern, $replacementFunction, $templateData);
return $parsedTemplate;
}
/**
* Callback function that returns value of a matching macro
* @param Array $matches
* @return String value of matching macro
*/
function parseMatchedText($matches)
{
if(isset($this->data[$matches[1]]))
{
return $this->data[$matches[1]];
}
return '{'.$matches[1].'}'; //wont be parsed if no value is set for the macro
}
} //End Of Class
?>
Usage Example
<?php
/**
* Usage of Simple Template Parser Class
*
* @author : MA Razzaque Rupom <rupom_315@yahoo.com>, <rupom.bd@gmail.com>
* Moderator, phpResource Group(http://groups.yahoo.com/group/phpresource/)
* URL: http://www.rupom.info
* @date : 07/17/2006
* @version : 1.0
* Purpose : Parsing Simple Template File and Data that Contains Macros
*/