NamePros
Welcome, Guest! Ready to make a name for yourself in the domain business? We welcome both the hobbyist and professional domainer to join the discussion as part of the NamePros community.

Click here to create your profile to start earning reputation for posting, and trader ratings for buying & selling in our free e-marketplace. Build your trader rating with each successful sale. Our system has tracked over 100,000 sales and counting!
FAQ & TOS Register Search Today's Posts Mark Forums Read

Go Back   NamePros.com > Website Development Discussion Forums > Programming > CODE
Reload this Page Output Class [PHP5]

CODE This forum is for posting code snippets and example scripts that aren't quite tutorials, but could be useful for others. You may post code snippets and/or completed scripts that you've written and want to share here.

Advanced Search


Closed Thread
 
LinkBack Thread Tools
Old 03-11-2008, 11:28 AM THREAD STARTER               #1 (permalink)
Senior Member
 
Dave's Avatar
Join Date: Jun 2007
Location: NamePros.com
Posts: 1,470
Dave has much to be proud ofDave has much to be proud ofDave has much to be proud ofDave has much to be proud ofDave has much to be proud ofDave has much to be proud ofDave has much to be proud ofDave has much to be proud ofDave has much to be proud ofDave has much to be proud of
 


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;
????: NamePros.com http://www.namepros.com/code/443220-output-class-php5.html
        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->varsEXTR_PREFIX_SAME"wddx");
        
        foreach(
$this->templates as $t)
        {
            include(
$this->path.$t.$this->extension);
????: NamePros.com http://www.namepros.com/showthread.php?t=443220
        }
    }
}
?>
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>
Dave is offline  
Closed Thread


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools


 
All times are GMT -7. The time now is 04:27 PM.

Domain name forum recommended by Domaining.com Powered by: vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.6.0 Ad Management plugin by RedTyger