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 - PHP4-5

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 04-20-2006, 08:24 PM THREAD STARTER               #1 (permalink)
 
BillyConnite's Avatar
Join Date: Jul 2005
Location: Coffs H, Australia
Posts: 3,456
BillyConnite has a reputation beyond reputeBillyConnite has a reputation beyond reputeBillyConnite has a reputation beyond reputeBillyConnite has a reputation beyond reputeBillyConnite has a reputation beyond reputeBillyConnite has a reputation beyond reputeBillyConnite has a reputation beyond reputeBillyConnite has a reputation beyond reputeBillyConnite has a reputation beyond reputeBillyConnite has a reputation beyond reputeBillyConnite has a reputation beyond repute
 


Wildlife Parkinson's Disease Parkinson's Disease

Talking PHP Template system - PHP4-5


Hey all,
????: NamePros.com http://www.namepros.com/code/189417-php-template-system-php4-5-a.html

Well I know it took me agggess to find a nice way to create a working template system in php, so loonngg ago I did some searching and editing and the whole works, and here's a simple template system... that works...

CREDIT: Most of code found at codewalkers.com - thanks codewalkers

functions.php:
PHP Code:
<?php

class Page
{
    function 
Page($template)
    {
        if (
file_exists($template))
        {
            
$this->page implode(''file($template));
        }
        else
        {
????: NamePros.com http://www.namepros.com/showthread.php?t=189417
            die(
"Template file: $template cannot be found.");
        }
    }

    function 
parse($file)
    {
        
ob_start();
        include(
$file);
        
$buffer ob_get_contents();
        
ob_end_clean();

        return 
$buffer;
    }

    function 
replace_tags($tags = array())
    {
        if (
sizeof($tags) > 0)
        {
            foreach (
$tags AS $tag => $data)
            {
                
$data = (file_exists($data)) ? $this->parse($data) : $data;
                
$this->page str_replace('{' $tag '}'$data$this->page);
            }
        }
    }

    function 
output()
    {
        echo 
$this->page;
    }
}

?>
index.php:
PHP Code:
<?php

require_once('template.class.php');

$tpl = new Page('design.html');
$tpl->replace_tags(array(
    
'title' => 'This is the title of my html page',
    
'content' => 'Here\'s some content which I am putting inside the page, yaaay.'
));
$tpl->output();

?>
design.html
Code:
<html>
<head>
<title>{title}</title>
</head>

<body>

<p>{content}</p>

</body>
</html>
And there you have it, a simple template system, that's quick, and WORKS!

Tested on PHP4 and PHP5 servers.

PS - 2ndV added a few (or should I say, took away a few lines of code also - Thanks).

All the best,
Rhett.
Last edited by BillyConnite; 07-22-2006 at 03:12 AM.
BillyConnite is offline  
Old 04-20-2006, 08:33 PM   #2 (permalink)
Senior Member
 
Eric's Avatar
Join Date: Mar 2005
Posts: 4,948
Eric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatness
 

Member of the Month
MOTM September 2005
Save a Life Child Abuse 9/11/01 :: Never Forget Baby Health Marrow Donor Program AIDS/HIV Breast Cancer Animal Rescue Cystic Fibrosis Ethan Allen Fund Animal Cruelty Ethan Allen Fund Ethan Allen Fund Baby Health Cancer Alzheimer's Protect Our Planet Cancer Survivorship SIDS Child Abuse Diabetes Protect Our Planet Multiple Sclerosis Autism Adoption Special Olympics
Just a note, really no need to use eregi_replace. str_replace will do the job just fine, and is faster than both ereg(i)_replace and preg_replace

And I'll include a quote from the php manual
Quote:
If you don't need fancy replacing rules (like regular expressions), you should always use this function instead of ereg_replace() or preg_replace().
Nice none-the-less.
Last edited by SecondVersion; 04-20-2006 at 08:36 PM.
Eric is offline  
Old 04-20-2006, 08:39 PM THREAD STARTER               #3 (permalink)
 
BillyConnite's Avatar
Join Date: Jul 2005
Location: Coffs H, Australia
Posts: 3,456
BillyConnite has a reputation beyond reputeBillyConnite has a reputation beyond reputeBillyConnite has a reputation beyond reputeBillyConnite has a reputation beyond reputeBillyConnite has a reputation beyond reputeBillyConnite has a reputation beyond reputeBillyConnite has a reputation beyond reputeBillyConnite has a reputation beyond reputeBillyConnite has a reputation beyond reputeBillyConnite has a reputation beyond reputeBillyConnite has a reputation beyond repute
 


Wildlife Parkinson's Disease Parkinson's Disease
Thanks 2ndver , I'll change it now, every little bit of optimization counts.

Just did a test on the two, and you're right 2ndver .
????: NamePros.com http://www.namepros.com/showthread.php?t=189417

This is with preg_replace:
Quote:
0.000887870788574 seconds
0.000899076461792 seconds
0.000828981399536 seconds
This is with str_replace:
Quote:
0.000752925872803 seconds
0.000725030899048 seconds
0.000748872756958 seconds
So str_replace is around 0.0009 seconds faster - Every little thing counts!
Last edited by BillyConnite; 04-20-2006 at 08:47 PM.
BillyConnite is offline  
Old 04-20-2006, 09:31 PM   #4 (permalink)
Resistance is Futile
 
Kadenz's Avatar
Join Date: Apr 2006
Location: Montreal, Canada
Posts: 1,094
Kadenz is a name known to allKadenz is a name known to allKadenz is a name known to allKadenz is a name known to allKadenz is a name known to allKadenz is a name known to all
 



Wildlife Lou Gehrig's Disease (ALS)
Thanks for the script, I will definately use it. You can almost make an entire CMS system based on that, sweet!
__________________
Freelance Web Developer
PHP, MySQL, XHTML, CSS, Javascript, jQuery, Wordpress
Portfolio: www.bundy.ca
Kadenz is offline  
Old 04-20-2006, 09:34 PM   #5 (permalink)
Senior Member
 
Eric's Avatar
Join Date: Mar 2005
Posts: 4,948
Eric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatness
 

Member of the Month
MOTM September 2005
Save a Life Child Abuse 9/11/01 :: Never Forget Baby Health Marrow Donor Program AIDS/HIV Breast Cancer Animal Rescue Cystic Fibrosis Ethan Allen Fund Animal Cruelty Ethan Allen Fund Ethan Allen Fund Baby Health Cancer Alzheimer's Protect Our Planet Cancer Survivorship SIDS Child Abuse Diabetes Protect Our Planet Multiple Sclerosis Autism Adoption Special Olympics
Originally Posted by BillyConnite
Thanks 2ndver , I'll change it now, every little bit of optimization counts.
????: NamePros.com http://www.namepros.com/showthread.php?t=189417

Just did a test on the two, and you're right 2ndver .

This is with preg_replace:

This is with str_replace:

So str_replace is around 0.0009 seconds faster - Every little thing counts!
No prob
Eric is offline  
Old 04-22-2006, 08:04 AM   #6 (permalink)
NamePros Regular
 
SB_GEMINI's Avatar
Join Date: Mar 2005
Location: Newport, UK
Posts: 930
SB_GEMINI is a splendid one to beholdSB_GEMINI is a splendid one to beholdSB_GEMINI is a splendid one to beholdSB_GEMINI is a splendid one to beholdSB_GEMINI is a splendid one to beholdSB_GEMINI is a splendid one to beholdSB_GEMINI is a splendid one to beholdSB_GEMINI is a splendid one to behold
 



thxs for this, will come in usefull
SB_GEMINI is offline  
Old 04-22-2006, 08:35 AM THREAD STARTER               #7 (permalink)
 
BillyConnite's Avatar
Join Date: Jul 2005
Location: Coffs H, Australia
Posts: 3,456
BillyConnite has a reputation beyond reputeBillyConnite has a reputation beyond reputeBillyConnite has a reputation beyond reputeBillyConnite has a reputation beyond reputeBillyConnite has a reputation beyond reputeBillyConnite has a reputation beyond reputeBillyConnite has a reputation beyond reputeBillyConnite has a reputation beyond reputeBillyConnite has a reputation beyond reputeBillyConnite has a reputation beyond reputeBillyConnite has a reputation beyond repute
 


Wildlife Parkinson's Disease Parkinson's Disease
No problem , hope it helps!
BillyConnite is offline  
Old 04-22-2006, 11:45 AM   #8 (permalink)
Senior Member
 
Camron's Avatar
Join Date: Jan 2006
Location: Portland, Oregon
Posts: 2,102
Camron has much to be proud ofCamron has much to be proud ofCamron has much to be proud ofCamron has much to be proud ofCamron has much to be proud ofCamron has much to be proud ofCamron has much to be proud ofCamron has much to be proud ofCamron has much to be proud of
 



VA Tech Memorial 9/11/01 :: Never Forget Cancer Survivorship Child Abuse
Sorry to ask. I am not a php expert. But what exactly would this do? What is the difference from making a phpinclude and just include it in pages?

Thanks,
Camron
__________________
HostingFuze.com Premium Master Reseller Services | 99.9% Uptime Guaranteed SLA | Starting at $4.95/mo
Basic Reseller Hosting @ HostFz.com - Services starting as low as $1.95/mo!
Camron is offline  
Old 04-22-2006, 12:22 PM   #9 (permalink)
NamePros Regular
 
Jim_'s Avatar
Join Date: Aug 2005
Location: NY, USA
Posts: 610
Jim_ is a splendid one to beholdJim_ is a splendid one to beholdJim_ is a splendid one to beholdJim_ is a splendid one to beholdJim_ is a splendid one to beholdJim_ is a splendid one to behold
 


Save The Children
Originally Posted by StackedTech
Sorry to ask. I am not a php expert. But what exactly would this do? What is the difference from making a phpinclude and just include it in pages?
????: NamePros.com http://www.namepros.com/showthread.php?t=189417

Thanks,
Camron
It parses the page, rather than including it. It also prevents php scripts from being embedding in the template.

Putting {TITLE} on a page is a lot safter than <?=$pagetitle?>
__________________
ask me about the internet
Jim_ is offline  
Old 04-22-2006, 06:39 PM THREAD STARTER               #10 (permalink)
 
BillyConnite's Avatar
Join Date: Jul 2005
Location: Coffs H, Australia
Posts: 3,456
BillyConnite has a reputation beyond reputeBillyConnite has a reputation beyond reputeBillyConnite has a reputation beyond reputeBillyConnite has a reputation beyond reputeBillyConnite has a reputation beyond reputeBillyConnite has a reputation beyond reputeBillyConnite has a reputation beyond reputeBillyConnite has a reputation beyond reputeBillyConnite has a reputation beyond reputeBillyConnite has a reputation beyond reputeBillyConnite has a reputation beyond repute
 


Wildlife Parkinson's Disease Parkinson's Disease
^^ What Jim said .

It's good too though so that when your selling a php script, and have a template system that parses the pages, the template can be written in 100% html or css so if the buyer has no php knowledge it is much easier and not confusing for them to create a template for the script than when using includes.

All the best, Rhett.
BillyConnite is offline  
Old 04-27-2006, 02:19 PM   #11 (permalink)
Senior Member

Join Date: Apr 2005
Location: .ma.us
Posts: 1,880
DropLister has much to be proud ofDropLister has much to be proud ofDropLister has much to be proud ofDropLister has much to be proud ofDropLister has much to be proud ofDropLister has much to be proud ofDropLister has much to be proud ofDropLister has much to be proud of
 


Baby Health Save a Life Save The Children
i used this code to make this network of minisites. www.treeguide.info
all of em use the design located at www.treeroots.info/design.html

Thanks man
__________________
DropLister is offline  
Old 04-28-2006, 01:54 AM THREAD STARTER               #12 (permalink)
 
BillyConnite's Avatar
Join Date: Jul 2005
Location: Coffs H, Australia
Posts: 3,456
BillyConnite has a reputation beyond reputeBillyConnite has a reputation beyond reputeBillyConnite has a reputation beyond reputeBillyConnite has a reputation beyond reputeBillyConnite has a reputation beyond reputeBillyConnite has a reputation beyond reputeBillyConnite has a reputation beyond reputeBillyConnite has a reputation beyond reputeBillyConnite has a reputation beyond reputeBillyConnite has a reputation beyond reputeBillyConnite has a reputation beyond repute
 


Wildlife Parkinson's Disease Parkinson's Disease
Originally Posted by droplister
i used this code to make this network of minisites. www.treeguide.info
????: NamePros.com http://www.namepros.com/showthread.php?t=189417
all of em use the design located at www.treeroots.info/design.html

Thanks man
Hey droplister,

No problem, glad it helped
BillyConnite is offline  
Old 05-15-2006, 01:20 PM   #13 (permalink)
NamePros Regular
 
edmarriner's Avatar
Join Date: Apr 2005
Location: England :D
Posts: 834
edmarriner is a jewel in the roughedmarriner is a jewel in the roughedmarriner is a jewel in the rough
 



Hi,

This template system is great, but im having trouble using php on the page.
Putting php in the array doesnt work.

e.g.
PHP Code:
'{CODE}' => 'php code here' 
????: NamePros.com http://www.namepros.com/showthread.php?t=189417
any tips?

thanks,

-ed
__________________
Leeds united football club


edmarriner is offline  
Old 05-15-2006, 04:22 PM THREAD STARTER               #14 (permalink)
 
BillyConnite's Avatar
Join Date: Jul 2005
Location: Coffs H, Australia
Posts: 3,456
BillyConnite has a reputation beyond reputeBillyConnite has a reputation beyond reputeBillyConnite has a reputation beyond reputeBillyConnite has a reputation beyond reputeBillyConnite has a reputation beyond reputeBillyConnite has a reputation beyond reputeBillyConnite has a reputation beyond reputeBillyConnite has a reputation beyond reputeBillyConnite has a reputation beyond reputeBillyConnite has a reputation beyond reputeBillyConnite has a reputation beyond repute
 


Wildlife Parkinson's Disease Parkinson's Disease
Hey ed,

What you could do is parse the php you want in the array first?

So instead of say:
'{CODE}' => '$variable=1+1*10'

You could do:
$variable=1+1*10;
'{CODE}' => $variable

Ofcourse it'd work with any functoins etc you wanted also... BUT I believe there is another way to do it, forgotten how though, I'll see if I can find it for you.
BillyConnite is offline  
Old 05-16-2006, 12:07 PM   #15 (permalink)
NamePros Regular
 
edmarriner's Avatar
Join Date: Apr 2005
Location: England :D
Posts: 834
edmarriner is a jewel in the roughedmarriner is a jewel in the roughedmarriner is a jewel in the rough
 



hi,

thanks, i never though about doing that, lol i feel stupid now

cheers,

-ed
__________________
Leeds united football club


edmarriner is offline  
Old 05-24-2006, 02:37 AM   #16 (permalink)
New Member
Join Date: May 2006
Posts: 3
crow is an unknown quantity at this point
 



I am trying to add images on same way but it wont show it.... DO i need to make some function to display images? t

$images="images/some.jpg"

to array i add
{IMG} => $images

to design.html

<p>{IMG}</p> but i just see error on line $images, and it only show "jpg" in this line where it call for images..

THnx
crow is offline  
Old 05-24-2006, 02:54 AM   #17 (permalink)
NamePros Regular
Join Date: Feb 2006
Posts: 528
psalzmann is just really nicepsalzmann is just really nicepsalzmann is just really nicepsalzmann is just really nice
 



Originally Posted by crow
I am trying to add images on same way but it wont show it.... DO i need to make some function to display images? t
????: NamePros.com http://www.namepros.com/showthread.php?t=189417

$images="images/some.jpg"

to array i add
{IMG} => $images

to design.html

<p>{IMG}</p> but i just see error on line $images, and it only show "jpg" in this line where it call for images..

THnx
try:

$images = "<img src='images/some.jpg' border='0' alt='' />";
{IMG} => $images



Regards,
Peter
psalzmann is offline  
Old 05-24-2006, 08:28 PM THREAD STARTER               #18 (permalink)
 
BillyConnite's Avatar
Join Date: Jul 2005
Location: Coffs H, Australia
Posts: 3,456
BillyConnite has a reputation beyond reputeBillyConnite has a reputation beyond reputeBillyConnite has a reputation beyond reputeBillyConnite has a reputation beyond reputeBillyConnite has a reputation beyond reputeBillyConnite has a reputation beyond reputeBillyConnite has a reputation beyond reputeBillyConnite has a reputation beyond reputeBillyConnite has a reputation beyond reputeBillyConnite has a reputation beyond reputeBillyConnite has a reputation beyond repute
 


Wildlife Parkinson's Disease Parkinson's Disease
Hey crow,

If the page is in a folder, then usually the image path will be wrong as compared with that folder... if you know what I mean.

So you'll have to try the full path i.e. http://domain.com/images/image.jpg instead of just images/image.jpg

Let me know how it goes,
Rhett.
BillyConnite is offline  
Old 05-24-2006, 10:40 PM   #19 (permalink)
Eating Pie
 
iNod's Avatar
Join Date: Nov 2004
Location: Canada
Posts: 2,272
iNod has much to be proud ofiNod has much to be proud ofiNod has much to be proud ofiNod has much to be proud ofiNod has much to be proud ofiNod has much to be proud ofiNod has much to be proud ofiNod has much to be proud ofiNod has much to be proud ofiNod has much to be proud of
 


Special Olympics AIDS/HIV Cystic Fibrosis Save The Children Baby Health Cystic Fibrosis
Nice work Billy.

- Steve
__________________
I feel old.
iNod is offline  
Old 05-26-2006, 01:07 AM   #20 (permalink)
Senior Member
 
bbalegere's Avatar
Join Date: Jul 2005
Location: Bangalore
Posts: 1,271
bbalegere is just really nicebbalegere is just really nicebbalegere is just really nicebbalegere is just really nicebbalegere is just really nice
 



Thanks a lot.
I will use it.
bbalegere is offline  
Old 05-26-2006, 01:32 AM   #21 (permalink)
Senior Member
 
Eric's Avatar
Join Date: Mar 2005
Posts: 4,948
Eric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatness
 

Member of the Month
MOTM September 2005
Save a Life Child Abuse 9/11/01 :: Never Forget Baby Health Marrow Donor Program AIDS/HIV Breast Cancer Animal Rescue Cystic Fibrosis Ethan Allen Fund Animal Cruelty Ethan Allen Fund Ethan Allen Fund Baby Health Cancer Alzheimer's Protect Our Planet Cancer Survivorship SIDS Child Abuse Diabetes Protect Our Planet Multiple Sclerosis Autism Adoption Special Olympics
I've edited it just a tad, if you don't mind me posting it here Billy
PHP Code:
<?php

class Page
{
    function 
Page($template)
    {
        if (
file_exists($template))
        {
            
$this->page implode(''file($template));
        }
        else
        {
            die(
"Template file: $template cannot be found.");
????: NamePros.com http://www.namepros.com/showthread.php?t=189417
        }
    }

    function 
parse($file)
    {
        
ob_start();
????: NamePros.com http://www.namepros.com/showthread.php?t=189417
        include(
$file);
        
$buffer ob_get_contents();
        
ob_end_clean();

        return 
$buffer;
    }

    function 
replace_tags($tags = array())
    {
        if (
sizeof($tags) > 0)
        {
            foreach (
$tags AS $tag => $data)
            {
                
$data = (file_exists($data)) ? $this->parse($data) : $data;
                
$this->page str_replace('{' $tag '}'$data$this->page);
            }
        }
    }

    function 
output()
    {
        echo 
$this->page;
    }
}

?>
Would use it nearly the same way as the original, however, no need to place {} around your tags in the replace_tags array. Also, no need to echo output()

Taken from Billy's original post, replacing w/my changes:
PHP Code:
<?php

require_once('template.class.php');

$tpl = new Page('design.html');
$tpl->replace_tags(array(
    
'title' => 'This is the title of my html page',
    
'content' => 'Here\'s some content which I am putting inside the page, yaaay.'
));
$tpl->output();

?>
design.html
Code:
<html>
<head>
<title>{title}</title>
</head>

<body>

<p>{content}</p>

</body>
</html>
Eric is offline  
Old 05-28-2006, 03:46 AM   #22 (permalink)
NamePros Regular
Join Date: Dec 2005
Posts: 210
wackyjoe is on a distinguished road
 



umm ok i need to make a news system udner one of my pages and im using this template system...the sites is kinda small since its for a school project

anyway so how would i go about adding the news system in the content area?

would i hve 2 make a completely new file with the news system and do:

$page = new Page("pages/news.php");
wackyjoe is offline  
Old 06-06-2006, 06:17 PM THREAD STARTER               #23 (permalink)
 
BillyConnite's Avatar
Join Date: Jul 2005
Location: Coffs H, Australia
Posts: 3,456
BillyConnite has a reputation beyond reputeBillyConnite has a reputation beyond reputeBillyConnite has a reputation beyond reputeBillyConnite has a reputation beyond reputeBillyConnite has a reputation beyond reputeBillyConnite has a reputation beyond reputeBillyConnite has a reputation beyond reputeBillyConnite has a reputation beyond reputeBillyConnite has a reputation beyond reputeBillyConnite has a reputation beyond reputeBillyConnite has a reputation beyond repute
 


Wildlife Parkinson's Disease Parkinson's Disease
Hey 2nV, thanks for the update - Original post updated with some credit for your help.

wackyjoe, im not quite sure what your getting at. This system would only be used for websites looking to maintin multiple templates, if your only going to use one, then there really is no need at all.

Rhett.
BillyConnite is offline  
Old 07-13-2006, 08:02 PM   #24 (permalink)
NamePros Member
Join Date: Dec 2005
Posts: 186
chronic will become famous soon enoughchronic will become famous soon enough
 



Is it possible to output alot of content and some PHP within the array? Would I have to do something like:

PHP Code:
echo <<<END
This uses the "here document" syntax to output
multiple lines with 
$variable interpolation. Note
????: NamePros.com http://www.namepros.com/showthread.php?t=189417
that the here document terminator must appear on a
line with just a semicolon. no extra whitespace!
END; 
__________________
DNFind.net - For sale, PM me.
chronic is offline  
Old 07-21-2006, 04:46 PM   #25 (permalink)
NamePros Member
 
blacksnday's Avatar
Join Date: Jun 2006
Location: ohio
Posts: 26
blacksnday is an unknown quantity at this point
 



Hrmm....
not to be any party pooper...
but this code looks remarkably similar to CodeWalkers Template Tutorial located at:
http://codewalkers.com/tutorials/58/8.html
__________________
WHERE'S MY RUM?!!
((in your hand...))
OH!
blacksnday is offline  
Closed Thread


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 02:32 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