Re: Re: Templates Are Driving me Nuts
| From: | Sean Malloy | Date: | Sat, 17 Jul 2004 13:48:15 +0000 |
| Subject: | Re: Re: Templates Are Driving me Nuts | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-190845@lists.php.net to get a copy of this message | ||
The template system I use is extremely simple.
Template.php:
<?php
class Template
{
var $Items;
var $_document;
function Template($document)
{
$this->Items = array();
$this->_document = $document;
}
function Render()
{
echo $this->ToString();
}
function ToString()
{
$template = $this->Items;
ob_start();
require($this->_document);
return ob_get_clean();
}
}
?>
and to use it?
templates/index.html:
<html>
<head></head>
<body>
Hello <?=$template['name']?>, it is currently <?=$template['time']?>
</body>
</html>
index.php:
<?php
include_once('Template.php');
$template = new Template('./templates/index.html');
$template->Items['name'] = 'User';
$template->Items['time'] = date('d M Y', time());
$template->Render();
?>
why use it?
Because you keep your html and your php code separate. That is the
goal when using any templating system.