- 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.
This is how to use it.
Ok, and a example of your .tpl/.html file would be...
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.
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...
You can use sortable tables and such, and still display them via this Class.
Hope you find it useful. :tu:
Rep' is appreciated.
Adrian
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







