A quick and dirty templating example - I've taken the liberty of changing your template slightly:
<?php
// mytemplate.php
class MyTemplate {
var $param;
var $template;
function myTemplate($template){
$this->param = array();
$this->template = file_get_contents($template);
}
function addParam($key, $value){
$this->param[$key] = $value;
}
function renderTemplate(){
foreach($this->param as $key => $value){
$this->template = str_replace("{%".strtoupper($key)."}", $value, $this->template);
}
}
function display(){
echo $this->template;
}
}
?>
<!--template.tpl:-->
<html>
<head>
<title>{%TITLE}</title>
</head>
<body>
{%BODY}
</body>
</html>
//Usage (stick in separate php files):
include($_SERVER['DOCUMENT_ROOT'] . "/mytemplate.php");
$template = new myTemplate("template.tpl");
$template->addParam("title", "MySiteTitle");
$template->addParam("body", "This is some content");
$template->renderTemplate();
$template->display();
If you want to store the result on the server, you would need a caching algorithm which is a bit more involved than simple templating.
Apologies if this is far from what you wanted, but it's early in the morning 🙂