The structure is as follows:
index.php, template.class.php in the root dir, then all the .tpl files
in templates/
OUTPUT:
This is a demo of the template class, the following content was
replaced into the file:
This is some random Content
This is the content from demo2.tpl
templates/demo.tpl:
This is a demo of the template class, the following content was
replaced into the file:
{{CONTENT}}
{{%demo2.tpl%}}
templates/demo2.tpl:
This is the content from demo2.tpl
index.php:
<?php
require_once("template.class.php");
$template = new template;
$template->dir("templates/");
$template->load("demo.tpl");
$template->prepare("CONTENT", "This is some random Content");
?>
template.class.php:
<?php
ob_start();
/////////////////////////////////
// (c)2006 Wolf Labs Design //
// http://www.wolflabs.org/ //
/////////////////////////////////
class template
{
private $html, $filenames, $time = array();
private $version = "2.4";
private $outputFilename = true;
private $dir, $lastFile;
public $errors = array();
function __construct()
{
$this->time["start"] = $this->_microtime_float();
}
public function outputFilename($bool)
{
$bool = strtolower($bool);
if($bool != true && $bool != false)
{
$this->errors[] = "Template Error: Invalid Input into
outputFilename function.";
}else{
$this->outputFilename = $bool;
}
}
public function load($file, $type = NULL)
{
if($type == NULL)
{
$type = uniqid("tpl_");
}
$this->filenames[] = $file;
$file = $this->dir.$file;
$this->html[$type] = file_get_contents($file);
$this->html[$type] = preg_replace('/\{\{%(.*?)%\}\}/e',
"template::_load('$1');", $this->html[$type]);
$this->lastFile = $type;
}
public function prepare($rep, $text, $type = NULL)
{
if(!$type)
{
$type = $this->lastFile;
}
$content = $this->html[$type];
$replace = '/\{\{'.$rep.'\}\}/is';
$content = preg_replace($replace, $text, $content);
$this->html[$type] = $content;
}
public function generic($content)
{
$type = uniqid("generic_");
$this->filenames[] = $type;
$this->html[$type] = $content;
$this->lastFile = $type;
}
public function setDir($dir)
{
$this->dir = $dir;
}
public function output()
{
$buffer = "";
$i = 0;
foreach($this->html as $html)
{
$buffer .= $html;
if($this->outputFilename)
{
$buffer .= "\n<!-- [EOF]: ". $this->dir . $this->filenames[$i] .
" -->\n";
}
$i++;
}
$this->time["end"] = $this->_microtime_float();
$time = $this->time["end"] - $this->time["start"];
$buffer .= "\n<!-- [Generation Time]: ". $time ." seconds -->";
$buffer .= "\n<!-- [Generator]: Template Class by Wolf Labs Design
(http://www.wolflabs.org) :: Version: ".$this->version." -->";
echo($buffer);
}
public function outputReturn()
{
$content = NULL;
foreach($this->html as $html)
{
$content .= $html;
}
return $content;
}
private function _microtime_float()
{
//From: php.net's php manual -- microtime function
list($usec, $sec) = explode(" ", microtime());
return ((float)$usec + (float)$sec);
}
private function _load($file)
{
$contents = file_get_contents($this->dir.$file);
return $contents;
}
}
?>
(http://www.wolflabs.org ) :: Version: ".$this->version." -->";
@Overall Topic: Oh also for anyone that tries to use the index.php
code, I made a mistake, $template->dir should be $template->setDir, I
didn't notice it till after I submitted it also I can't seem to find a
place where I can edit my post :P
Also I don't seem to be making a very good name for myself at this
point, yet again another error for my crappy index.php file (no I did
not test it before I threw it up there because I do not want to post
the source to one of the real sites that uses that engine). The file
should have $template->output(); at the end, or if you want it
returned to a variable, $var = $template->outputReturn();