- Impact
- 5,506
PHP:
<?php
/*********************************************************************************
* Very basic class that handles sending output to the browser including necessary
* templates and headers. Useful for separating business logic from presentation
* logic.
*********************************************************************************
* @author David Parr <[email protected]>
* @copyright David Parr 2008
* @license [url]http://opensource.org/licenses/gpl-license.php[/url] GNU Public License
* @version 0.1
*/
class Output
{
// @var String The path to the templates
private $path = "";
// @var String The extension of the file
private $extension = "";
// @var Array Additional headers.
private $headers = array();
// @var Array Templates to be outputted i.e. header, footer etc.
private $templates = array();
// @var Array Variables. Variables for each template.
private $vars = array();
/**
* Constructor sets path and extension for template files.
*/
public function __construct($path, $extension = ".php")
{
// Path must be a directory.
if(is_dir($path) && is_readable($path))
{
$this->extension = $extension;
$this->path = $path;
}
else
{
throw new Exception("Invalid path \"".$path."\" specified");
}
}
/**
* Set an additional header.
*/
public function setHeader($header)
{
$this->headers[] = $header;
}
/**
* Set another template. Make sure it exists within the template directory.
*/
public function setTemplate($template)
{
$templatePath = $this->path.$template.$this->extension;
if(is_readable($templatePath))
{
$this->templates[] = $template;
}
else
{
throw new Exception("Invalid template \"".$template.$this->extension."\" specified");
}
}
/**
* Sets a new associative array of variables for use in templates.
*/
public function setVars($vars)
{
$this->vars = array_merge($this->vars, $vars);
}
/**
* Process headers and templates.
*/
public function process()
{
// First of all send headers before processing output.
foreach($this->headers as $h)
{
header($h);
}
// Extract the vars. This is so we don't have to use $this in the templates but direct variable names
extract($this->vars, EXTR_PREFIX_SAME, "wddx");
foreach($this->templates as $t)
{
include($this->path.$t.$this->extension);
}
}
}
?>
There is an example below.
PHP:
<?php
// You can use an autoload functions but for now I will just include the output class
include("class/output.php");
// Absolute path to the templates directory. Make sure include trailing slash friend like I do below.
$path = "/path/to/templates/goes/here/";
// INITIALISE!!! Use your own extension if you want (like .tpl.php or .template.php)
$output = new Output($path);
// ... GET MY DATA FROM DB HERE OR SOMETHING ...
// .. AND THEN MAGIC.... THE VARS FOR YOUR TEMPLATES
$vars = array(
"title" => "This is example of Output!",
"test" => "test"
);
// We can set as many headers/templates/vars we want!
// BUT REMEMBER THIS. TEMPLATES ARE OUTPUTTED IN THE ORDER YOU INDEX THEM!
$output->setVars($vars);
$output->setTemplate("example");
$output->process();
?>
Our example template looks like this.
PHP:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title><?php echo $title ?></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<h1><?php echo $test ?></h1>
</body>
</html>