[advanced search]
NamePros Domain Auction
Live Event This Thursday at 6PM EDT - Prebidding open now
29 members in the live chat room. Join Chat!
Register Rules & FAQ NP$ Store Active Threads Mark Forums Read
Go Back   NamePros.Com > Design and Development > Programming > CODE
User Name
Password

Old 03-11-2008, 11:28 AM   · #1
DomainManDave
NamePros Regular
 
Trader Rating: (5)
Join Date: Jun 2007
Posts: 497
NP$: 59.10 (Donate)
DomainManDave is just really niceDomainManDave is just really niceDomainManDave is just really niceDomainManDave is just really nice
Cancer
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>


Please register or log-in into NamePros to hide ads
DomainManDave is offline   Reply With Quote
Reply

NamePros is a revenue sharing forum.

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump


Site Sponsors
www.clixnhits.com Venta de dominios Compra tu dominio Venta de dominios Compra tu dominio
Advertise your business at NamePros
All times are GMT -7. The time now is 11:15 AM.


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