- Impact
- 109
Hey all,
Well I know it took me agggess to find a nice way to create a working template system in php, so loonngg ago I did some searching and editing and the whole works, and here's a simple template system... that works...
CREDIT: Most of code found at codewalkers.com - thanks codewalkers :D
functions.php:
index.php:
design.html
And there you have it, a simple template system, that's quick, and WORKS! :D
Tested on PHP4 and PHP5 servers.
PS - 2ndV added a few (or should I say, took away a few lines of code also - Thanks).
All the best,
Rhett.
Well I know it took me agggess to find a nice way to create a working template system in php, so loonngg ago I did some searching and editing and the whole works, and here's a simple template system... that works...
CREDIT: Most of code found at codewalkers.com - thanks codewalkers :D
functions.php:
PHP:
<?php
class Page
{
function Page($template)
{
if (file_exists($template))
{
$this->page = implode('', file($template));
}
else
{
die("Template file: $template cannot be found.");
}
}
function parse($file)
{
ob_start();
include($file);
$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 = str_replace('{' . $tag . '}', $data, $this->page);
}
}
}
function output()
{
echo $this->page;
}
}
?>
index.php:
PHP:
<?php
require_once('template.class.php');
$tpl = new Page('design.html');
$tpl->replace_tags(array(
'title' => 'This is the title of my html page',
'content' => 'Here\'s some content which I am putting inside the page, yaaay.'
));
$tpl->output();
?>
design.html
Code:
<html>
<head>
<title>{title}</title>
</head>
<body>
<p>{content}</p>
</body>
</html>
And there you have it, a simple template system, that's quick, and WORKS! :D
Tested on PHP4 and PHP5 servers.
PS - 2ndV added a few (or should I say, took away a few lines of code also - Thanks).
All the best,
Rhett.
Last edited: