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 PHP Template System 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


Closed Thread
 
LinkBack Thread Tools
Old 03-21-2007, 10:30 AM THREAD STARTER               #1 (permalink)
Senior Member
 
Hitch's Avatar
Join Date: Aug 2005
Location: Uk, South Yorkshire
Posts: 1,228
Hitch is a splendid one to beholdHitch is a splendid one to beholdHitch is a splendid one to beholdHitch is a splendid one to beholdHitch is a splendid one to beholdHitch is a splendid one to beholdHitch is a splendid one to behold
 


Winner
PHP Programming - May 2007
Animal Rescue Third World Education Find Marrow Donors!

Cool PHP Template System Class


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 Code:
<?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.
????: NamePros.com http://www.namepros.com/code/307704-php-template-system-class.html
PHP Code:
<?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 Code:
<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 Code:
<?php

require "tpl_class.php";
????: NamePros.com http://www.namepros.com/showthread.php?t=307704

$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 Code:
<?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.
Rep' is appreciated.

Adrian
Hitch is offline  
Old 03-21-2007, 11:17 AM   #2 (permalink)
Senior Member
 
Barrucadu's Avatar
Join Date: Aug 2005
Location: East Yorkshire, England
Posts: 2,689
Barrucadu is a splendid one to beholdBarrucadu is a splendid one to beholdBarrucadu is a splendid one to beholdBarrucadu is a splendid one to beholdBarrucadu is a splendid one to beholdBarrucadu is a splendid one to beholdBarrucadu is a splendid one to behold
 




Nice simple class, rep++
Barrucadu is offline  
Old 03-21-2007, 02:17 PM   #3 (permalink)
NamePros Regular
 
beaver6813's Avatar
Join Date: May 2005
Location: England
Posts: 390
beaver6813 is a jewel in the roughbeaver6813 is a jewel in the roughbeaver6813 is a jewel in the rough
 




Taken some of the techniques from your script

PHP Code:
while($allowed_to_give_rep==0){
echo 
"Waiting to be allowed to give Hitch rep";
????: NamePros.com http://www.namepros.com/showthread.php?t=307704
sleep(60);
check_allowed();
}
$Rep++; 
Last edited by beaver6813; 03-21-2007 at 03:26 PM.
beaver6813 is offline  
Old 03-21-2007, 02:58 PM THREAD STARTER               #4 (permalink)
Senior Member
 
Hitch's Avatar
Join Date: Aug 2005
Location: Uk, South Yorkshire
Posts: 1,228
Hitch is a splendid one to beholdHitch is a splendid one to beholdHitch is a splendid one to beholdHitch is a splendid one to beholdHitch is a splendid one to beholdHitch is a splendid one to beholdHitch is a splendid one to behold
 


Winner
PHP Programming - May 2007
Animal Rescue Third World Education Find Marrow Donors!
Sam, Lol.

Thanks anyway.
Hitch is offline  
Old 04-07-2007, 10:27 PM   #5 (permalink)
NamePros Regular
 
CharlieA's Avatar
Join Date: Jan 2007
Posts: 376
CharlieA is a jewel in the roughCharlieA is a jewel in the roughCharlieA is a jewel in the rough
 



Nice and simple
__________________
Turn Your Parked Domains Into Stores!
<< High Paying Cost Per Click

Make A Pay Per Click Store For Your Parked Domains!
<< Hassle Free and Customizable
CharlieA is offline  
Old 04-09-2007, 09:05 AM   #6 (permalink)
Account Suspended
 
Alex.'s Avatar
Join Date: Nov 2006
Location: Uk
Posts: 601
Alex. is on a distinguished road
 


Ethan Allen Fund Third World Education
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"); ?
Alex. is offline  
Closed Thread


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


 
All times are GMT -7. The time now is 04:27 PM.

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