Dynadot Aftermarket: discover domain gems worth investing in. Visit our aftermarket.

PHP Template System Class

Namecheap AuctionsNamecheap Auctions
Namecheap AuctionsNamecheap Auctions
Spacemail by SpaceshipSpacemail by Spaceship
Watch

Hitch

Established Member
Impact
74
Hey,

So, i made this Class a while ago, for a project, then decided to post it on my blog, and now, here. :)

It's the sort of system PhpBB uses.
You can use .html/.tpl files etc, and use the {tags}

You can use variables for "replacement" too.
Check out the class, then i will explain how to use it.

PHP:
<?php

//-----------------------------------------------------------------
// Template System Class
// Made by Adrian Crepaz at Dreamit.co.uk

// Feel free to use this code in your own scripts.
// For information on how to use the Class, see here, http://www.dreamit.co.uk/php-template-system-class/
// Please do not re-distribute this code seperatly, or as your own
//-----------------------------------------------------------------

class Tpl
{
	var $file;	

	function file($user_file)
	{
		if(file_exists($user_file))
		{
			$this->file = file_get_contents("" . $user_file . "");
		}
		else
		{
			die ("Sorry, the following file, " . $user_file . " could not be found.");
		}
	}

	function replacement($tags = array())
	{
		if(count($tags) == 0)
		{
			die ("No tags defined for Template Tag replacement.");
		}
		else
		{
			foreach($tags AS $tag => $replace)
			{
				$this->file = str_replace("{" . $tag . "}", $replace, $this->file);
			}
		}
	}
	
	function display()
	{
		echo $this->file;
	}
}

$tpl = new tpl;

?>

This is how to use it.
PHP:
<?php

require "tpl_class.php";

$tpl->file("template_design.html");

$tpl->replacement(array(
	"sitename" => "Dream It",
	"slogan" => "The Technology Blog",
	"footer" => "Copyright to Adrian at Dreamit.co.uk"
));

$tpl->display();

?>

Ok, and a example of your .tpl/.html file would be...
HTML:
<h2>Welcome to {sitename}</h2>
{slogan}
<hr>
Hello, this would be some content.
<hr>
{footer}

When i said variables earlier, you could use them like this. (Lets get the Sitename from the MySql DB, the sort of thing to use in a CMS)
Also, lets say we want to use a specific "theme/template", which the template name/path is stored in the DB.

PHP:
<?php

require "tpl_class.php";

$get_settings = mysql_query("SELECT * FROM `settings`") or die ("Cannot get settings.");
$setting = mysql_fetch_array($get_settings);

$tpl->file("templates/" . $setting['style'] . "/header.tpl");

$var = "This var could also be used in the Replacement.";

$tpl->replacement(array(
	"sitename" => $setting['sitename'],
	"slogan" => "The Technology Blog",
	"footer" => "Copyright to Adrian at " . $setting['sitename'] . ""
));

$tpl->display();

?>

Last example, lets say you have the function in your CMS/Site, to enable/disable the CMS.
Like vBulletin...
Right, lets say, our "Disabled Page" is called offline.tpl

You could use...
PHP:
<?php

require "tpl_class.php";

$get_settings = mysql_query("SELECT * FROM `settings`") or die ("Cannot get settings.");
$setting = mysql_fetch_array($get_settings);

if($setting['online'] == 0)
{
	$tpl->file("templates/" . $setting['style'] . "/offline.tpl");
} else if ($setting['online'] == 1)
{
	$tpl->file("templates/" . $setting['style'] . "/index.tpl");
}

$tpl->replacement(array(
	"sitename" => $setting['sitename'],
	"slogan" => "The Technology Blog",
	"footer" => "Copyright to Adrian at " . $setting['sitename'] . ""
));

$tpl->display();

?>

You can use sortable tables and such, and still display them via this Class.

Hope you find it useful. :tu:
Rep' is appreciated. :)

Adrian
 
4
Close
The views expressed on this page by users and staff are their own, not those of NamePros.
GoDaddyGoDaddy
Nice simple class, rep++
 
0
Close
Taken some of the techniques from your script ;)

PHP:
while($allowed_to_give_rep==0){
echo "Waiting to be allowed to give Hitch rep";
sleep(60);
check_allowed();
}
$Rep++;
 
Last edited:
0
Close
Sam, Lol.

Thanks anyway. :)
 
0
Close
Nice and simple :)
 
0
Close
Ah, thanks, a nice and simple template class.
The smarty one made by the php people really confuses me :S

btw, if I wanted to include the files, header.tpl, body.tpl and footer.tpl, would I have to have multiple instances of $tpl->file("file"); ?
 
0
Close
DomainEasy: flexible LTO options. Close more domain deals with financing.

We're social

Escrow.com
Spaceship
Domain Recover
CryptoExchange.com

NamePros updates

Member card: see how many threads a member started
The member card now shows a Threads number, right after Posts. The member card is the box that opens when you point at a name. Profile...
AIVikings
Catchy
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back