Here's a class for you that I wrote for my own uses, it reads fast template style templates where you encapsulate your output items in curly braces such as : {var}
it also allows you to set variable into the global scope via a template by using the set command as follows: {SET:VARIABLE=VALUE}
You can then call that variable as $GLOBALS['VARIABLE']
NOTE: The class is intended for use with Constants and GLOBALS, though you can pass in arrays as well.
The order of priority is GLOBAL -> CONSTANT -> Array So be careful with your naming.
An additional nicety is you can have the C style comment / / and it will strip it out.
I could make that feature a bit more robust, but it was just an afterthought and haven't gotten around to the other style comment, but you can have as many astericks after the slash as you want and it should work, handy for adding comments that you want private amongst developers within a template.
Like I said, I made it for my own use and it's handy for me, maybe you'll find it to your liking as well.
If you're looking for something more standard, you might want to check out fastTemplates.
anyway... here it is.
<? class tagParse {
/*******************************************************************************
tagParse.obj
Author: Van Carney (van@webfreshener.com)
©2000-2002 Webfreshener.com
last rev: 2/25/02 10:30:23 AM
Wrapper object for Template Parsing
*******************************************************************************/
function Parse_Template($template, $varName) {
/
This Method calls Read_Template and scans the
file buffer output for {TEMPLATE_DIRECTIVES} if a pattern match is found,
the method then replaces the directive with an evaluated PHP variable
/
$this->pFile = $this->file2string($template,'','');
$this->Parse_Template_Vars();
define($varName,$this->parsedFile);
unset($this->parsedFile);
}
function PUT_PAGE($fileLocation) {
/
handy function to compile
a standalone page such as a small
pop-up window or other custom item
that has it's own layout and no
unparsed nested items.
/
// Load the page...
// -- note: echo will not print before flush() is called
// since Output Buffering is being used.
$this->Parse_Template($fileLocation, 'READY_PAGE');
echo READY_PAGE;
}
function Make_Result_List($resultsArray,$template,$varName) {
/
Builds a results listing from a template
for display within a page
This is usually an HTML table
containing search results
/
if (is_array($resultsArray)) {
// Set our template
$this->pFile = $this->file2string($template,'','');
foreach($resultsArray as $key=>$val) {
foreach($resultsArray[$key] as $nKey=>$nVal) {
$this->resultList[0][$nKey] = $nVal;
}
$GLOBALS['PARSE_LOOP_INT'] = sprintf($GLOBALS['PARSE_LOOP_INT']+1);
$this->Parse_Template_Vars($template);
$myResultList .= $this->parsedFile;
} // and do it again...
}
//write it out as a global for later use...
define($varName, $myResultList);
}
/*******************************************************************************
SubRoutines
-- These are the functions used by the above methods
******************************************************************************/
function Parse_Template_Vars() {
/
This function will scan the template
for a pattern match based upon {a-zA-Z0-9-_}
if it finds a match, it will replace the pattern
with a predefined constant.
*/
$this->parsedFile = $this->pFile;
//Find all {TEMPLATE_DECLARATIONS} and {SET: VARIABLES}
preg_match_all('/{([\/a-z0-9:=.\s_-]{1,})}/i',
$this->parsedFile,$template_vars);
for ($i=0;$i<count($template_vars[1]);$i++) {
//make sure we're not dealing with a SET declaration...
if (!preg_match('/set:+\s([a-z0-9-]{1,})\s=+\s*([\/a-z0-9,!.\s-]{1,})/i',
$template_vars[1][$i],$set_vars)) {
//lets look for it in the $GLOBALS[] first...
if (isset($GLOBALS[$template_vars[1][$i]])) {
$textVar = "\$newVar = \$GLOBALS['".$template_vars[1][$i]."'];";
}
// start looking for CONSTANTS...
elseif (defined($template_vars[1][$i])) {
$textVar = "\$newVar = constant('".$template_vars[1][$i]."');";
} else { // then is an array value or is empty...
$textVar = "\$newVar = '".$this->resultList[0][$template_vars[1][$i]]."';";
}
//now Parse the file
@eval($textVar);
$this->parsedFile = @ereg_replace($template_vars[0][$i],
$newVar,$this->parsedFile);
} else { //then we are setting GLOBAL VALUES
$GLOBALS[$set_vars[1]] = $set_vars[2];
$this->parsedFile=@ereg_replace($template_vars[0][$i],'',$this->parsedFile);
}
}
//Strip out PHP style comments....
$this->parsedFile = @ereg_replace('/+*+.**+/+','',$this->parsedFile);
}
////////////////////////////////////////////////////////////////////////////////
} // end Class
?>