I'm trying to make a better template system for one of my script and I need help.
Its based with .tpl files a processor and the php file.
What I have in my processor file is:
<?php
class Page
{
var $page;
function Page($template) {
if (file_exists($template))
$this->page = join("", file($template));
else
die("Template file $template not found.");
}
function parse($file) {
ob_start();
include($file);
$this->page = eregi_replace("{if", "<?php if", $this->page);
$this->page = eregi_replace("--if}", " {", $this->page);
$this->page = eregi_replace("{/if}", "} ?>", $this->page);
$buffer = ob_get_contents();
ob_end_clean();
return $buffer;
}
function replace_tags($tags = array()) {
if (sizeof($tags) > 0)
foreach ($tags as $tag => $data) {
$data = (file_exists($data)) ? $this->parse($data) : $data;
$this->page = eregi_replace("{" . $tag . "}", $data,
$this->page);
//this does what I'm trying to do
$this->page = eregi_replace("{if", "<?php \r\n if", $this->page);
$this->page = eregi_replace("--if}", " {", $this->page);
$this->page = eregi_replace("{/if}", "} ?>", $this->page);
}
else
die("No tags designated for replacement.");
}
function output() {
echo $this->page;
}
}
?>
In my php file I have:
<?
//Gets the script name --> template name should be the same
$this_script = $_SERVER["SCRIPT_NAME"];
$this_script = array_pop(explode("/", $this_script));
list($this_script, $ext) = explode(".", $this_script);
//echo $this_script;
require_once("template/system.php");
$page = new Page("./template/".$this_script.".tpl");
$page->replace_tags(array(
"test" => "HOME",
"descript" => "Welcome to my website!",
"main" => "dat/index.dat",
"menu" => "dat/menu.dat",
"left" => "dat/submenu.dat",
"right" => "dat/right.dat",
"footer" => "dat/footer.php"
));
$page->output();
?>
and in my tpl file I have:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>
<body>
<!--This is processed-->
{test}
<!--This needs to be processed-->
{if ({test}=={test}) --if}
echo "test";
{/if}
</body>
</html>
What appears in my php file after everything is processed is this:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>
<body>
HOME
<?php
if (HOME==HOME) {
echo "test";
} ?>
</body>
</html>
What I need is to process the PHP sintax in the output