<?php
/**
* @package Googolplex
*/
/**
* Templater class.
*
* Creates pages using a user defined template.
*
* @author Janus Jacob Rønne <janusjr@tdcadsl.dk>
* @copyright The PHP License, version 3.0 ({@link
http://www.php.net/license/3_0.txt Read license})
* @package Googolplex
* @version 1.0
*/
class Templater
{
/**
* Content items to be insertet into the template.
*
* @access private
* @var array;
*/
private $Content;
/**
* User defined delimiters (left & right) used in template.
*
* @access private
* @var array;
*/
private $Delimiters;
/**
* Class constructor.
*
* @access public
* @param array|string $startDelimiter
* @param string $endDelimiter
*/
public function __construct($startDelimiter = null, $endDelimiter = null)
{
$this->Content = array();
if (!isset($startDelimiter))
{
$this->Delimiters = array('start' => '[*', 'end' => '*]');
}
else if (is_array($startDelimiter))
{
$this->Delimiters['start'] = current($startDelimiter);
$this->Delimiters['end'] = next($startDelimiter);
}
else
{
$this->Delimiters['start'] = $startDelimiter;
$this->Delimiters['end'] = $endDelimiter;
}
}
/**
* Assigns value(s) to template variable(s).
*
* @access public
* @param array|string $templateVar
* @param string $value
*/
public function assign($templateVar, $value = null)
{
if (is_array($templateVar))
{
foreach ($templateVar as $key => $value)
{
$this->Content[$key] = $value;
}
}
else
{
$this->Content[$key] = $value;
}
}
/**
* Displays the completed page.
*
* @access public
* @param string $templatePath
*/
public function display($templatePath)
{
$page = file_get_contents($templatePath);
foreach ($this->Content as $key => $value)
{
$item = $this->Delimiters['start'] . $key . $this->Delimiters['end'];
$page = str_replace($item, $value, $page);
}
echo $page;
}
}
?>
Den er til PHP5, men nem at omskrive.