Template Class, need some opinions on it

0 views
Skip to first unread message

Ankoku

unread,
Apr 12, 2007, 5:19:13 PM4/12/07
to OC PHP
Well this is a template class that I wrote a while back and use on
most of my sites. I was wondering if anyone had some ideas on anything
else that I should add/change on it. If my demo sucks, please tell me
and I'll see if I can make it less crappy.

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;
}
}
?>

Craig Gardner

unread,
Apr 13, 2007, 12:57:37 PM4/13/07
to Ankoku, OC PHP
Hi!

I looked at your template and thought it would probably be nice for you if it had some kind of way to loop through arrays. Another nice thing you may want to consider is some other basic formatting functions and maybe tidying the HTML before output depending on what you are trying to do with it.

Craig

(http://www.wolflabs.org ) :: Version: ".$this->version." -->";

Ankoku

unread,
Apr 13, 2007, 4:01:26 PM4/13/07
to OC PHP
Can you please specify what you would like to see be able to loop
through arrays?

@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

Message has been deleted

Craig Gardner

unread,
Apr 13, 2007, 7:16:35 PM4/13/07
to OC PHP
The ability to loop through an array similarly to foreach(), etc.

Ankoku

unread,
Apr 13, 2007, 10:05:11 PM4/13/07
to OC PHP
What I ment was, would you like to see the load() or prepare() or what
be able to run though arrays supplied to them.

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();

Reply all
Reply to author
Forward
0 new messages