|
|
|
|
|
|
| |
This class implements a simple template processor engine based on regular expression matching.
It can process template strings or files.
It uses regular expressions to find place holder marks and a callback function to replace the marks by the respective template parameter values.
TemplateParser.class.php
| <?php
/**
* 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
*/
class TemplateParser
{
var $data;
/**
* Initializes "macro=>value" array
* @param Array "macro=>value" array
* @return none
*/
function initData($data)
{
$this->data = array();
$this->data = $data;
}
/**
* 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
*/
require_once "TemplateParser.class.php";
$data = array();
$data['first_name'] = "MA";
$data['last_name'] = "Razzaque";
$data['nick_name'] = "Rupom";
$data['password'] = "supersecret";
$data['sender_name'] = "Administrator";
$data['site_name'] = "Name_Of_Site";
$obj = new TemplateParser();
$obj->initData($data);
//Parse Template File
$parsedData = $obj->parseTemplateFile("template.html");
echo $parsedData;
echo "<br>-----------------------------------<br>";
//Parse Template Data
$parsedData = $obj->parseTemplateData(file_get_contents("template.html"));
echo $parsedData;
?> | | |
|
| YellowPages Content Grabber (PHP5 +) Categories : PHP, PHP Classes, Regexps, Databases, MySQL | | | The GTV.class allows you to extract a value between any HTML tag or between any TEXT on a web page. Categories : PHP, PHP Classes, Regexps | | | Form is a utility class for generating html forms. It provides form initialization and regex based data validation (both server and client side) with a convenient interface. This version obsoletes version 1.0a Categories : HTML, PHP, PHP Classes, Regexps | | | FormChecker Package - validate any data via classes and patterns.
Categories : PHP, Form Processing, PHP Classes, Regexps | | | Expose - PHP template engine, supports server and client-sided caching,a plugin system, multiple languages, template script language is based on PHP itself. Categories : PHP, PHP Classes, Templates, Complete Programs | | | Simple Template Class/Example Categories : PHP, Templates, PHP Classes | | | Very minimal templating engine Categories : PHP, PHP Classes, Templates | | | file class , uploade file , download file already uploaded on another website Categories : PHP, PHP Classes, Filesystem, Web Services | | | PHP Paypal IPN Integration Class v1.0.0 Categories : PHP, PHP Classes, Payment Gateways | | | crop and resize image class using gd library function Categories : PHP, PHP Classes, GD image library, Graphics | | | A Timing Class Categories : PHP, PHP Classes, Date Time | | | The class to check load time of your script
VERY usefull for relatively slow applications, but not only.. Categories : PHP, PHP Classes, Debugging | | | Avoiding or Detecting high bit characters in a string. Useful when you want to create a valid RSS feed Categories : PHP, Strings, Unicode, Regexps, Rich Site Summary (RSS) | | | Create HTML forms dynamicly using Javascript & PHP Categories : PHP, PHP Classes, Java Script | | | usercounter class Categories : PHP, PHP Classes, Databases, MySQL, Environment Variables | |
|
|
|