NamePros.Com (http://www.namepros.com/)
-   CODE (http://www.namepros.com/code/)
-   -   Output Class [PHP5] (http://www.namepros.com/code/443220-output-class-php5.html)

DomainManDave 03-11-2008 11:28 AM

Output Class [PHP5]
 
PHP Code:
<?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 <david@scoph.com>
* @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 Code:
<?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 Code:
<!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>


All times are GMT -7. The time now is 06:12 AM.
Site Sponsors
Advertise your business at NamePros

Powered by: vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 2.4.0