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 PHP5 Template Class

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


Reply
 
LinkBack Thread Tools
Old 09-24-2008, 06:53 AM THREAD STARTER               #1 (permalink)
Senior Member
 
Dave's Avatar
Join Date: Jun 2007
Location: NamePros.com
Posts: 1,400
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 of
 


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)
????: NamePros.com http://www.namepros.com/code/517342-php5-template-class.html
    {
        
$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->dataEXTR_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;
????: NamePros.com http://www.namepros.com/showthread.php?t=517342
            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!
Dave is offline   Reply With Quote
Old 10-13-2008, 08:03 AM   #2 (permalink)
First Time Poster!
Join Date: Oct 2008
Posts: 1
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
Old 12-01-2008, 10:50 PM   #3 (permalink)
NamePros Regular
 
DylanButler's Avatar
Join Date: Jan 2006
Location: San Diego, CA
Posts: 735
DylanButler is a splendid one to beholdDylanButler is a splendid one to beholdDylanButler is a splendid one to beholdDylanButler is a splendid one to beholdDylanButler is a splendid one to beholdDylanButler is a splendid one to beholdDylanButler is a splendid one to beholdDylanButler is a splendid one to behold
 



Elegant example!

The next step could be to create your own template language and eliminate that PHP from the templates altogether..

Code:
<html>
<head>
<title>##$title##</title>
</head>
<body>
##if $name##
Hello ##$name##, welcome to ##$sitename##!
##/if##
</body>
But really this is a good job and hopefully I will use it.
DylanButler is offline   Reply With Quote
Old 12-06-2008, 07:04 PM   #4 (permalink)
First Time Poster!
Join Date: Dec 2008
Posts: 1
BlakeB is an unknown quantity at this point
 



I'm having the same issue with the "title" variable. Any ideas why this is happening?
BlakeB is offline   Reply With Quote
Old 12-06-2008, 08:04 PM THREAD STARTER               #5 (permalink)
Senior Member
 
Dave's Avatar
Join Date: Jun 2007
Location: NamePros.com
Posts: 1,400
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 of
 


Cancer

Update

Ok guys give this update a try. Should work fine now.

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
* @version 07/12/2007
*/

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 gets a template variable.
     *
     * @param string $key
     * @return mixed
     */
    
public function __get($key)
????: NamePros.com http://www.namepros.com/showthread.php?t=517342
    {
       return 
$this->data[$key];
    }
    
????: NamePros.com http://www.namepros.com/showthread.php?t=517342
    
/**
     * 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->dataEXTR_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;
    }
}
?>
Thanks!
Dave is offline   Reply With Quote
Reply


LinkBacks (?)
LinkBack to this Thread: http://www.namepros.com/code/517342-php5-template-class.html
Posted By For Type Date
php - Lightweight PHP5 based template class/system - Stack Overflow This thread Refback 11-04-2011 04:03 PM
php - Lightweight PHP5 based template class/system - Stack Overflow This thread Refback 10-31-2011 10:05 AM
php - templates - Lightweight PHP5 based template class/system - SimpleAnswer This thread Refback 01-23-2011 03:18 AM
Lightweight PHP5 based template class/system - Stack Overflow This thread Refback 02-10-2010 12:56 AM
Lightweight PHP5 based template class/system - Stack Overflow This thread Refback 02-10-2010 12:49 AM
Lightweight PHP5 based template class/system - Stack Overflow This thread Refback 02-10-2010 12:49 AM

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


Liquid Web Smart Servers  
All times are GMT -7. The time now is 03:11 AM.

Managed Web Hosting by Liquid Web
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