Loading Templates
<?php
function LoadTemplate($file="")
{
$strfile = "$doc_root/".$file;
if(!file_exists($strfile)) print"Loading template failed!";
$thisfile = file($strfile);
while(list($line,$value) = each($thisfile)) {
$value = ereg_replace("(r|n)","",$value);
$result .= "$valuern";
}
return
$result;
}
?>
Replacing Statics
<?php
function ReplaceStatic($LoadTemplate="", $StaticName="", $StaticValue="")
{
$tcontent = $LoadTemplate;
$j = count($StaticName);
for($i=0;$i<$j;$i++) {
$tcontent = eregi_replace($StaticName[$i],$StaticValue[$i],$tcontent);
}
return
$tcontent;
}
?>
$LoadTemplate
, this variable is the output of the function taken from LoadTemplate();
. Thus to output some value on it you have to do some $LoadTemplate=LoadTemplate();
and we’re sure that this will have a value of a flat HTML template loaded from your LoadTemplate function. $StaticName
, this variable is the one which can be found on your HTML template, a sample of this varialble looks like the following; , ,
, etc., we observed that this is a comment syntax from HTML. These are the following which should be replaced with some values after the parsing has been done. The values could be “Polerio and PHPBUILDER”, “Article about parsing”, “Some Fixed Values”, etc. The values are then stored at $StaticValue
variable. Perhaps you’d think already that this function is flexible because you only have to put what variable you want to use from your HTML template and encode/ replace it with values taken/generated from your PHP script. This is flexible in a sense that you’re not limited to a fixed varialble, you can specify two or more variables into your HTML template.
How can we put it into action? We will use arrays. Thus to select what variables to be used and what values to be encoded into the given variables we will use a code that looks like the following;
<?php
$StaticName = array(
"<!--%ModuleTitle%-->",
"<!--%SomeComment%-->",
"<!--%FixedValues%-->"
);
$StaticValue = array(
"Polerio and PHPBUILDER",
"Article about parsing",
"Some Fixed Values"
);
$output = ReplaceStatic($LoadTemplate, $StaticName, $StaticValue);
echo $output;
?>
$output
to finally generate its result. Now continuing to a more complex parsing, let’s proceed to dynamic parsing. The values to be encoded on HTML templates are dynamic which only means the result are more than one.