Sr. Web Developer
mediabistro.com
US-NY-New York

Justtechjobs.com Post A Job | Post A Resume

PHP Templates: Revisited
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 .= "$value\r\n";
    }

    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 \r\n thus making your HTML template imploded with \r\n. After replacing the \r\n 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.
Next Page

[Page 1]  [Page 2]  


Comments:
Constuctive Critisismscottybwoy08/30/07 05:50
PHP Programming,- text-based gameScott D05/02/07 17:28
Improvement to access to SubsectionsShai Lipkovich06/11/05 14:57
Contact php File to replace Mail@Peter Brennan02/16/05 03:19
Template helpAisha D02/04/05 12:19
All smarty isDumbass10/02/04 22:01
Templates live where you dare not treadBenjamin Smith04/24/04 03:10
agreed:RE: Templates are DEAD! Long live PHP!Roy Robin12/19/03 13:52
RE: Templates are DEAD! Long live PHP!Hobbit06/08/03 16:19
TemplateTamerDavorin Rusevljan01/05/03 10:17
RE: Templates are DEAD! Long live PHP!L long12/17/02 16:32
Introducing {PHPTMPL}Geraud Krawezik11/18/02 07:59
RE: Templates are DEAD! Long live PHP!Sonic_Molson11/15/02 22:04
RE: Templates are DEAD! Long live PHP!Patrick Fitzgerald11/03/02 12:57
Wow... Needs lots of improvementeruanno10/22/02 09:52
RE: Templates are DEAD! Long live PHP!Michael Sinclair10/14/02 15:07
RE: Templates are DEAD! Long live PHP!Harry Fuecks10/09/02 06:05
This is what I would doJoe10/07/02 02:33
RE: Templates are DEAD! Long live PHP!Michael Sinclair10/06/02 08:31
RE: Templates are DEAD! Long live PHP!haj10/02/02 02:44
RE: Whats wrong with FastTemplate or phplibhaj10/02/02 02:36
RE: Templates are DEAD! Long live PHP!M. Thornton09/30/02 14:55
RE: Templates are DEAD! Long live PHP!Harry Fuecks09/29/02 08:13
RE: Templates are DEAD! Long live PHP!bigSys09/28/02 11:48
RE: Templates are DEAD! Long live PHP!c-dawg09/27/02 19:35
PHP replication of Zope and JSP for templatigMichael Glazer09/27/02 17:49
RE: Don't be too criticalBDKR09/27/02 11:06
RE: Templates are DEAD! Long live PHP!Adalberto09/27/02 10:54
RE: NEED HELP URGENTLY, PLEASEadd php project09/25/02 05:49
NEED HELP URGENTLY, PLEASEEddieson09/24/02 12:00
RE: Templates are DEAD! Long live PHP!dotwebbie09/23/02 11:40
The best template engine is SmartyOskars09/21/02 16:32
patTemplateMitchell Landry09/21/02 02:14
RE: Templates are DEAD! Long live PHP!Harry Fuecks09/20/02 06:45
RE: Whats wrong with FastTemplate or phplibZpiff09/20/02 04:32
RE: Templates are DEAD! Long live PHP!Yuriy Horobey09/19/02 02:31
Templates are DEAD! Long live PHP!Harry Fuecks09/18/02 12:21
RE: Whats wrong with FastTemplate or phplibanimosity09/18/02 12:19
Don't be too criticalJason09/18/02 09:21
RE: why notTom Anderson09/16/02 12:06
Uhm... ya.. ok...Vincent09/15/02 08:38
one more thingAivar Annamaa09/15/02 04:34
ComplexTemplateAivar Annamaa09/15/02 03:55
RE: why notClaudio Bustos09/14/02 16:26
RE: why notkybosh09/13/02 19:54
why notPhynias09/13/02 11:22
Whats wrong with FastTemplate or phplibZpiff09/11/02 08:24
why not simply use patTemplate or alike?NightOwl09/11/02 02:41
just use vlibTemplateKelvin Jones09/10/02 04:31
Ignore this article: save brain cells.Tom Anderson09/09/02 15:23
 

If you are looking for help, please post on the appropriate forum here. Your questions will be answered much more quickly.

Add A Comment:

Name:

Email:

Subject:

Message:

To reduce spam posts, messages are now manually approved

You are not [logged in]. That means your account will not get credit for this post.