Results 1 to 8 of 8
Threaded View
-
1st Jul 2010, 04:35 PM #1OPMemberWebsite's:
litewarez.net litewarez.com triniwarez.comCreating a Fast PHP Template System
Well, I really dont see the point in a template engine that uses repalcements/regex so i have made this tutorial to show you another way.
PHP Is already a template engine, when you write <?php echo $var?> its just like doing <{$var}> or {$var}
Think of it this way, PHP Already translates <?php echo '<b>hello</b>'?> into <b>hello</b> by its engine, so why make it do everything 2 times over.
The way you would implement a template engine is like so
------
Firstly create a template class
PHP Code:class Template
{
var $vars = array();
function __set($key,$val)
{
$this->vars[$key] = $val;
}
function __get($key)
{
return isset($this->vars[$key]) ? $this->vars[$key] : false;
}
function output($tpl = false)
{
if($tpl === false)
{
die('No template file selected in Template::output(...)');
}
if(!file_exists(($dir = 'templates/' . $tpl . '.php')))
{
die(sprintf('Tpl file does not exists (%s)',$dir));
}
new TemplateLoader($dir,$this->vars);
return true;
}
}
----------
And then create a standalone class to compile the tpl file within.
PHP Code:class TemplateLoader
{
private $vars = array();
private $_vars = array(); //hold vars set within the tpl file
function __construct($file,$variables)
{
$this->vars = $variables;
//Start the capture;
ob_start();
include $file;
$contents = ob_get_contents();
ob_end_clean(); //Clean it
//Return here if you wish
echo $contents;
}
function __get($key)
{
return isset($this->vars[$key]) ? $this->vars[$key] : (isset($this->_vars[$key]) ? $this->_vars[$key] : false) : false;
}
function __set($key,$val)
{
$this->_vars[$key] = $val;
return true;
}
function bold($key)
{
return '<strong>' . $this->$key . '</string>';
}
}
----------
#Index.php
PHP Code:<?php
require_once 'includes/Template.php';
require_once 'includes/TemplateLoader.php';
$Template = new Template();
$Template->foo = 'somestring';
$Template->bar = array('some' => 'array');
$Template->zed = new stdClass(); // Showing Objects
$Template->output('index'); // loads templates/index.php
?>
----------
templates/index.php
PHP Code:header
<h1><?php $this->foo;?></h1>
<ul>
<?php foreach($this->bar as $this->_foo):?>
<li><?php echo $this->_foo; ?></li>
<?php endforeach; ?>
</ul>
<p>Testing Objects</p>
<?php $this->sidebar = $this->foo->show_sidebar ? $this->foo->show_sidebar : false;?>
<?php if($this->sidebar):?>
Showing my sidebar.
<?php endif;?>
footer
----------
NOTE: IN the TemplateLoader Class you can add a function like..
PHP Code:function bold($key)
{
return '<strong>' . $this->$key . '</string>';
}
You still have all the normal tools such as stripslashes/htmlentites etc.
Heres a small example of the bold.
PHP Code:$this->bold('foo'); //Returns <strong>somestring</string>
You can add lots of tools into the TempalteLoader class such as inc() to load other tpl files, you can develop a helper system so you can go $this->helpers->jquery->googleSource
If you have any more questions feel free to ask me.litewarez Reviewed by litewarez on . Creating a Fast PHP Template System Well, I really dont see the point in a template engine that uses repalcements/regex so i have made this tutorial to show you another way. PHP Is already a template engine, when you write <?php echo $var?> its just like doing <{$var}> or {$var} Think of it this way, PHP Already translates <?php echo '<b>hello</b>'?> into <b>hello</b> by its engine, so why make it do everything 2 times over. The way you would implement a template engine is like so ------ Rating: 5Join Litewarez.net today and become apart of the community.
Unique | Clean | Advanced (All with you in mind)
Downloads | Webmasters
Notifications,Forum,Chat,Community all at Litewarez Webmasters
Sponsored Links
Thread Information
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)
Similar Threads
-
PHP PDO login system, user class. Also Includes a theming system {Source}
By xifyhosting in forum Web Development AreaReplies: 3Last Post: 29th Aug 2012, 02:33 AM -
[Help] Creating a Template but something is missing? Help Asap
By Laz0r in forum Graphics AreaReplies: 6Last Post: 30th Nov 2011, 03:00 AM -
Need fast DLE template
By John in forum Webmaster DiscussionReplies: 6Last Post: 22nd Nov 2011, 04:16 PM -
Creating a simple Object Config system in PHP
By litewarez in forum Web Development AreaReplies: 0Last Post: 9th Dec 2010, 09:59 PM -
Creating a website with a template engine! (Detailed Tutorial for newbies)
By litewarez in forum Tutorials and GuidesReplies: 6Last Post: 28th Sep 2009, 08:46 PM
themaPoster - post to forums and...
Version 5.38 released. Open older version (or...