#native_company# #native_desc#
#native_cta#

PHP Templates: Revisited Page 2

By Polerio Babao
on September 9, 2002

Loading Templates

To load a template free from line feeds and carriage returns this function should be used.

<?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;

}

?>



This function basically loads your HTML template. We have a variable “$doc_root”, if you’re using windows you probable have (c:/htdocs/) this kind of working directory. If you’re in Linux system, you have /usr/local/apache or /var/www/html/ etc., file system. Thus select or copy-paste the location of your template and put it into your doc_root. If the loading is successful, this function will strip all of your carriage return and line feed characters as specified by rn thus making your HTML template imploded with rn. After replacing the rn with an empty value this function will then return its output stored in $result variable.

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;

}

?>



This function simply replaces the variable from your template. A variable that occurs only ones, for instance title of the table, title of the site, etc. From the function we have a variable $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.
We’ve see that there’s another variable in this function which is $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;

?>



I’ve only added $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.

1
|
2
|
3
|
4
|
5
|
6
|
7