Hey,
I'd recommend using str_replace instead as it's proven to be 25% faster that the preg/ereg (replace) funcs.
I recently created a very basic template system for a client. Although the goal is different the concept is the same. And it doesn't take much to get it working:
file: tmpl.class.php
<?php
class tmpl
{
// Global Variables
var $tmpl_type;
var $tmpl_dir;
var $tmpl_file;
var $tmpl_var_tags;
var $tmpl_func_tags;
var $tmpl_inc_tags;
var $tmpl_data;
// function: tmpl ()
//
// Main constructor for class tmpl. Sets up the environment.
function tmpl()
{
$this->tmpl_type = "html";
$this->tmpl_dir = "/root2/include/php4/tmpl";
return 1;
}
// function: read_tmpl ( string tmpl_file, [ string tmpl_dir ] )
//
// Reads in the template file and sets up the requested tags
function read_template( $tmpl_file,$tmpl_dir='/tmp' )
{
if( ! $tmpl_file )
{
die("function: read_tmpl() requires a template filename be passed to it.");
}
else
{
if( ! is_file($tmpl_file) )
{
return 0;
}
}
if( $tmpl_dir != $this->tmpl_dir )
{
if( ! is_dir($tmpl_dir) )
{
return 0;
}
}
$tmpl_fp = fopen("$tmpl_dir/$tmpl_file","r");
while( ! feof($tmpl_fp) )
{
if( $tmpl_data )
{
$tmpl_data .= fgets($tmpl_fp,4096);
}
else
{
$tmpl_data = fgets($tmpl_fp,4096);
}
}
fclose($tmpl_fp);
preg_match("/<VAR NAME=([A-za-z0-9\.+]) \/>/",$tmpl_data,$tmpl_re_matches);
foreach( $tmpl_re_matches as $tag => $value )
{
$this->add_tag($tag);
}
$this->tmpl_data = $tmpl_data;
return 1;
}
// function: set_tag_var ( string tag_name, string tag_content )
//
// Set tag $tag_name to $tag_content.
function set_tag_var( $tag_name,$tag_content )
{
if( ! $tag_name )
{
die("function: set_tag_var() requires a tag name passed to it.");
}
if( ! $tag_content )
{
if( ! $tag_name == "QUERY_STRING" )
{
die("function: set_tag_var() requires tag content be passed to it.");
}
}
$this->tmpl_var_tags[$tag_name] = $tag_content;
return 1;
}
// function: output ()
//
// Output's the whole template with tags filled in.
function output()
{
if( ! $this->tmpl_data )
{
die("function: output() cannot be called before read_template().");
}
$tmpl_out = $this->$tmpl_data;
foreach( $this->tmpl_var_tags as $tag => $value )
{
if( $value == 1 )
{
$this->tmpl_var_tags[$tag] = "Empty Tag ($tag) value.";
}
$this->tmpl_data = str_replace("<VAR NAME=$tag />",$this->tmpl_var_tags[$tag],$this->tmpl_data);
}
echo $this->tmpl_data;
return 1;
}
// End Class: tmpl
}
?>
file: test-tmpl.php
<?php
// change the error_reporting prefs:
error_reporting(E_ERROR|E_WARNING|E_PARSE);
// require the tmpl class (tmpl.class.php):
require_once("/home/exposu/public_html/elfyn/tmpl.class.php");
// create a new tmpl object:
$tmpl = new tmpl();
// read in the template file (test.tmpl):
$tmpl->read_template("test.tmpl","/home/exposu/public_html/elfyn");
// template variables (e.g. <VAR=xyz />):
$_VARS[TITLE] = "Welcome to my site!";
$_VARS[META_DESCRIPTION] = "Elfyn McBratney's personal homepage.";
$_VARS[META_KEYWORDS] = "elfyn,mcbratney,web,site";
$_VARS[REMOTE_URL] = "http://".$SERVER_NAME.$REQUEST_URI;
if( $QUERY_STRING != "" ) { $_VARS[REMOTE_URL] .= "?".$QUERY_STRING; }
$_VARS[STYLE_BGCOLOR] = "#ffffff";
$_VARS[STYLE_TEXT] = "#000000";
$_VARS[STYLE_LINK] = "#0099ff";
$_VARS[STYLE_FONTFACE] = "verdana,arial,helvetica,geneva";
$_VARS[STYLE_FONTSIZE] = 2;
$_VARS[STYLE_FONTCOLOR] = "#000000";
$_VARS[WELCOME_MSG] = "Welcome to my site. "."\n";
$_VARS[WELCOME_MSG] .= "Click here to visit \n";
$_VARS[WELCOME_MSG] .= "<A HREF=\"http://www.exposure.org.uk\">exposure.org.uk</A>";
foreach( $_VARS as $tag => $value )
{
$tmpl->set_tag_var($tag,$value);
}
$_VARS = array();
unset($_VARS);
// output the content to the browser
$tmpl->output();
?>
file: test.tmpl
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE><VAR NAME=TITLE /></TITLE>
<META NAME="title" CONTENT="<VAR NAME=TITLE />" />
<META NAME="description" CONTENT="<VAR NAME=META_DESCRIPTION />" />
<META NAME="keywords" CONTENT="<VAR NAME=META_KEYWORDS />" />
<META NAME="remote_url" CONTENT="<VAR NAME=REMOTE_URL />" />
</HEAD>
<BODY BGCOLOR="<VAR NAME=STYLE_BGCOLOR />" TEXT="<VAR NAME=STYLE_TEXT />" LINK="<VAR NAME=STYLE_LINK /> VLINK="<VAR NAME=STYLE_LINK /> VLINK="<VAR NAME=STYLE_LINK />">
<P>
<FONT FACE="<VAR NAME=STYLE_FONTFACE />" SIZE="<VAR NAME=STYLE_FONTSIZE />" COLOR="<VAR NAME=STYLE_FONTCOLOR />">
<VAR NAME=WELCOME_MSG />
</FONT>
</P>
</BODY>
</HTML>
All you need to di is edit the the default paths in tmpl.class.php and the re's to match your tag format, and integrate the functions used in test-tmpl.php to replace your tags.