.: Templating system :.
|
Author: Albert |
|
|
Download: Template Merge sytem [ 15-6-2004 ] 2.3 kB
Globals OFF compatible |
Templating in PHP, wow that sounds like a headache, and only
for the pro's.
Actually it is not and let me show how its done.
There are several (20++) templating engines to be found.
In my case i wanted a simple templating engine that would also work with PHP
template files.
Would be very very simple, and suit its purpose of saving layout time.
So you make the layout one time and use it for xxx pages.
Well i always want something extra....
First the basic of templates.....
A template is a html file with the variables included as $xxx or whatever you
may choose.
You create a template in which variables are represented as $myvar.
some HTML here....
<p align="left">$page->body</p>
some more HTML
|
You save this file as template (ordinairy htm)
Now we go and call the script:
In the script we define the variables like:
$page->title = "My title";
$page->name = "Albert"
and if for sample $page->body is a big html thinggy, well just put it in
a file and save on disk.
$page->body = read_file("Myfile_Body.htm").
where read_file() is defined in the template engine.
Now we use the 'template engine'....
In here we read the template from disk into a variable say $content.
Now we replace all $..... in $content with their actual value
eval("\$content = \"" . str_replace("\"","\\\"",$content)
. "\";"); |
that is the only statement we need.
That is all, now we can = echo $content; =
Your template will be displayed with all the variables.
Now the complicated way.
I wanted templates that can contain variables and or php structures.
Because you might have a simple layout in which you 'include' files.
This gives one complication for the template engine,
It might contain variables which are not defined for replacement.
So what to do........
Well in the template replace a variable that you dont define in the calling
script with ##myvar.
Where myvar is the name, this instead of $myvar.
sample of template
<?
§§pageview=0; <---- this
variable i dont want merged
include ("../bit_include.php");
include ("../rate/rateit.php");
§§cachetimeout=-1; require("../cache/jpcache.php");
<---- this variable i dont want merged
?>
<html>
<head>
<title>Pondok PHP Scripts</title>
<meta http-equiv="Content-Type" content="text/html;
charset=iso-8859-1">
<script language='javascript' type='text/javascript' src='popwin.js'>
</script>
.................
<tr>
<td bgcolor="#00FFFF" class="small">
<? include ("filedata.php");
filedata('$download->filename','$download->descr','$download->short');
<-- these are variables to merge
if ($page->globalsoff == 1){
echo'
<img src="colordot.gif" width="14"
height="14" align="absmiddle">
Globals OFF compatible';
}
?>
</td>
</tr>
..............
This actually represents:
<?
$pageview=0; <---- this variable
i dont want merged
include ("../bit_include.php");
include ("../rate/rateit.php");
$cachetimeout=-1; require("../cache/jpcache.php");
<---- this variable i dont want merged
?>
<html>
<head>
<title>Pondok PHP Scripts</title>
<meta http-equiv="Content-Type" content="text/html;
charset=iso-8859-1">
<script language='javascript' type='text/javascript' src='popwin.js'>
</script>
.................
<tr>
<td bgcolor="#00FFFF" class="small">
<? include ("filedata.php");
filedata('$download->filename','$download->descr','$download->short');
<-- these are variables to merge
if ($page->globalsoff == 1){
echo'
<img src="colordot.gif" width="14"
height="14" align="absmiddle">
Globals OFF compatible';
}
?>
</td>
</tr>
..............
|
You use the template engine, all variables are now replaced.
But you still have to replace back §§ to $.
I took §§ because it can be represented in html as §
So do it, and write $content as a file to disk.
$handle= fopen ($callfile."1.php","w+b");
fwrite ($handle,$content);
fclose($handle);
// $callfile is the name of the script that calls the
merge engine add '1' to get unique name |
In the calling script include the just saved file.
include ("mergetemplate.php"); <-- the
template engine
$file=str_replace(".php","",$_SERVER[PHP_SELF]);
$file=substr($file,strrpos($file,"/")+1);
$page->title = "My title";
$page->name = "Albert"
$page->body = read_file("Myfile_Body.htm").
// call the engine
$body = merge_template($file,file_get('template.html'),$page,$file);
This is a portion of the
merge engine
$callfile='../templates/'.$callfile;
# Return the merged main_template from this function
# but first put back $ signs from ##
$content = str_replace("§§","$",$content);
$handle= fopen ($callfile."1.php","w+b");
fwrite ($handle,$content);
fclose($handle);
// and include the written result
include ($file."1.php");
// VOILA DONE
|
And yes.......the whole thing appears with all the requested variables inside.
You can even go further, you can write the merged template into a cache dir
and do a check whether the fiel already exists.
If the file already exists, you dont have to merge the file (unless certain
data change).
So now you have complete templating engine that works......
Just download the file and you will see how little is needed to achieve a wonder!
This page has been made with it........
|