PHP include files or Templates?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

JPlush76
Forum Regular
Posts: 819
Joined: Thu Aug 01, 2002 5:42 pm
Location: Los Angeles, CA
Contact:

PHP include files or Templates?

Post by JPlush76 »

I was talking to someone this week about coding and he asked if I use templates for layout design and i said no, I just used Include files for the header/footer/and side bars insead. He looked at me like i had 4 eyes.

I see that phpbb using template files for layout, is there a difference in design/performance or are they two different ways of doing the same thing?

thanks :)
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Post by hob_goblin »

there are four methods off the top of my head:

1) including header.html footer.html sidebar.html etc...

2) including layout.php:

(layout.php)

Code: Select all

<html>
 stuff
 <?=$content;?>
</html>
into each file, and making a file look like:

afile.php

Code: Select all

<?
$content = <<<HTML
here is some html..
HTML;
include('layout.php');
?>
this method is very useful if you are just doing html...including php gets difficult

3) having a base.php file something like:

base.php

Code: Select all

<?
function siteheader()&#123;
echo 'this is a header';
&#125;
function sitefooter()&#123;
echo 'this is a footer';
&#125;
?>
and files looking like:

afile.php

Code: Select all

<?
include('base.php');
siteheader();
echo 'this is something inbetween';
sitefooter();
?>
4) phpbb's method, having template files containing things like {thing} and using regular expressions to replace them... this was designed for people who know html, but not php... and to me, it seems like it would be slower
User avatar
Takuma
Forum Regular
Posts: 931
Joined: Sun Aug 04, 2002 10:24 am
Location: UK
Contact:

Post by Takuma »

I think the way phpBB does it is very clever as it might be a bit slower than others but easy for people to edit. And I do the same. :lol:
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post by nielsene »

I use a class based version of hob_goblin's method three. I've set up three related files on my server as phps files if you want to take a look.

done.php
prime.php
HTMLDisplay.inc
(I apoligize for the unreadable orange used for comments... I've never used phps's before and don't know if you can customize the coloring to make it more readable.)

The first two are "Displayable Pages" ie pages that will be seen by the user, done.php is a very simple file, that destroys the session. It used to also do some logic, but that's been moved to a cronjob now. Prime.php is a simple registration form, but you'll see that it uses most of the same elements as done.php.

The final one is the main display class I use (UIBase did nothing in this version of the code, i'e changed that in my development version). The first third of the file and the last 1/4 or so, is what should be in the class, some of the middle stuff is more logic heavy and doesn't really belong there... I'm hoping to change that in the future.

This version of templating is probably one of the slowest out there. (phpBB style could be slow if implemented with slow regexpressions, but normally will be faster. Pure includes are always faster. I'm willing to take the hit to have a code architecture that feels good/solid to me.

If you want to poke around at more of the files, you can download the whole thing at sf.net/projects/compinabox.
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Post by hob_goblin »

yes, you can customize the colors of phps, its in one of the config files i think.

second off, i see you have that bug mentioned before where the script gets cut off, either that or you have no "?>" at the end..
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post by nielsene »

Ooh, you're right, sloppy coding on my part. no close ?> (the phps is not cut off)
JPlush76
Forum Regular
Posts: 819
Joined: Thu Aug 01, 2002 5:42 pm
Location: Los Angeles, CA
Contact:

Post by JPlush76 »

isn't it amazing how color coding makes life easier :)
User avatar
9902468
Forum Commoner
Posts: 89
Joined: Thu Jun 06, 2002 6:39 am
Location: Europe

Post by 9902468 »

Im using a system where everything is parsed through index.php. Index.php takes care of session, authentication, variable passing, error handling, displaying correctly constructed menu to user, (depending on user rights) etc. Now I only have to write content files where I tell the heading of the page, needed javascript services, needed other services and the content (Like login or add new loaner.) , and who can use the page ie. admins, users, unauthenticated etc. Also I try to write as little as possible html; all is done in functions, this way I get standardized layout, less code etc.

Anyone else using system like this? Or have I done another template system? What do you think?

-9902468
User avatar
Takuma
Forum Regular
Posts: 931
Joined: Sun Aug 04, 2002 10:24 am
Location: UK
Contact:

Post by Takuma »

9902468 - I don't use those system but I made a template class like phpBB by the way how can you remember that username? :lol: .
JPlush76
Forum Regular
Posts: 819
Joined: Thu Aug 01, 2002 5:42 pm
Location: Los Angeles, CA
Contact:

Post by JPlush76 »

lol Takuma, I hope he has auto-login checked off :)
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Post by hob_goblin »

cookies
JPlush76
Forum Regular
Posts: 819
Joined: Thu Aug 01, 2002 5:42 pm
Location: Los Angeles, CA
Contact:

Post by JPlush76 »

cake
User avatar
Takuma
Forum Regular
Posts: 931
Joined: Sun Aug 04, 2002 10:24 am
Location: UK
Contact:

Post by Takuma »

What you talking about hot_goblin and JPlush76?

Pie...
JPlush76
Forum Regular
Posts: 819
Joined: Thu Aug 01, 2002 5:42 pm
Location: Los Angeles, CA
Contact:

Post by JPlush76 »

mmmm apple pie with cake and cookies

the best of all worlds :twisted:
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Post by hob_goblin »

Takuma wrote:9902468 - I don't use those system but I made a template class like phpBB by the way how can you remember that username? :lol: .
http://www.php.net/setcookie
Post Reply