[advanced search]
Results from the most recent live auction are here.
10 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 09-24-2008, 06:53 AM   · #1
DomainManDave
NamePros Regular
 
Location: snezo.com
Trader Rating: (13)
Join Date: Jun 2007
Posts: 755
NP$: 1.95 (Donate)
DomainManDave is a name known to allDomainManDave is a name known to allDomainManDave is a name known to allDomainManDave is a name known to allDomainManDave is a name known to allDomainManDave is a name known to all
Cancer
Smile PHP5 Template Class

Hello again,

This is a very lightweight and very easy to use template class for PHP5. Supports sub templates using a nice little magical method.

Template Class

PHP Code:
<?php
/**
* Basic template engine for PHP5. Not bloated
* and just gets the job done.
*
* @package Template
* @author David Parr <dave@snezo.com>
* @copyright Copyright (c) David Parr, 2008
*/

class Template
{
    
// Template name
    
protected $name;
    
    
// Local and global data. Gl
    
protected $data = array();
    
    
/**
     * Constructor sets the template name, and makes sure
     * it exists.
     *
     * @param string The template name
     */
    
public function __construct($name)
    {
        if( !
is_file(TEMPLATE_PATH . $name . TEMPLATE_EXT))
            die(
'Invalid template: ' . $name);
            
        
$this->name = $name;
    }
    
    
/**
     * Magically set some template data.
     *
     * @param string Key of the data
     * @param string Value of the data
     */
    
public function __set($key, $value)
    {
        
$this->data[$key] = $value;
    }
    
    
/**
     * Magically render a template. Great for sub templates.
     *
     * @return string
     */
    
public function __toString()
    {
        
// Need the new line character to format output correctly :)
        
return $this->render() . "\n";
    }
    
    
/**
     * Renders a template.
     *
     * @param bool Should the template be directly printed out?
     * @return string
     */
    
public function render($print = FALSE)
    {
        
// Begin output buffering
        
ob_start();
        
        
// Extract data to local namespace. Don't worry extract isn't great
        // but this is only in local scope so nothing to worry about :)
        
extract($this->data, EXTR_SKIP);
        
        
// We do it like this so we can use the direct name of the variable
        // rather than having $this everywhere in your templates
        
require_once(TEMPLATE_PATH . $this->name . TEMPLATE_EXT);
        
        
$output = ob_get_clean();
        
        if(
$print === TRUE)
        {
            echo
$output;
            return
true;
        }
        
        return
$output;
    }
}
?>


Example

PHP Code:
<?php
require_once('classes/Template.php');

// Template path with a trailing slash and extension of template files with a dot prefix.
// This lets you decide where to place your templates and what extension to give them i.e. '.template.php', '.tpl.php'
// or whatever suits your needs.
define('TEMPLATE_PATH', dirname(__FILE__) . '/templates/');
define('TEMPLATE_EXT', '.php');

// Create a home template, with header and footer as sub templates
$home = new Template('home');
$home->message = 'Hello world';

$home->header = new Template('header');

// This is how you set a variable in a sub template. Easy as pie :)
$home->header->title = 'Home Page';

$home->footer = new Template('footer');

$home->render(TRUE);

?>


Header

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>


Home

PHP Code:
<?php echo $header; ?>
    <h1><?php echo $message; ?></h1>
<?php echo $footer; ?>


Footer

PHP Code:
</body>
</
html>


All the above will produce..

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></title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> </head> <body> <h1>Hello world</h1> </body> </html>


Thanks and enjoy!


Please register or log-in into NamePros to hide ads
DomainManDave is offline   Reply With Quote
Old 10-13-2008, 08:03 AM   · #2
Websters
First Time Poster!
 
Trader Rating: (0)
Join Date: Oct 2008
Posts: 1
NP$: 0.00 (Donate)
Websters is an unknown quantity at this point
What am I missing?

Why is it the $home->header-title doesn't show up as I would expect?

Even in your example output above the $title is not set - is that intentionally or am I missing something very basic here?

Thanks!
Websters 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
Traffic Down Under Website Header Design Website Header Design
Advertise your business at NamePros
All times are GMT -7. The time now is 08:41 AM.


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