Page 1 of 2

PHP include files or Templates?

Posted: Fri Aug 23, 2002 1:49 pm
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 :)

Posted: Fri Aug 23, 2002 2:47 pm
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

Posted: Fri Aug 23, 2002 3:06 pm
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:

Posted: Fri Aug 23, 2002 3:18 pm
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.

Posted: Fri Aug 23, 2002 3:46 pm
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..

Posted: Fri Aug 23, 2002 3:54 pm
by nielsene
Ooh, you're right, sloppy coding on my part. no close ?> (the phps is not cut off)

Posted: Fri Aug 23, 2002 4:08 pm
by JPlush76
isn't it amazing how color coding makes life easier :)

Posted: Sat Aug 24, 2002 10:49 am
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

Posted: Sat Aug 24, 2002 2:43 pm
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: .

Posted: Sat Aug 24, 2002 2:48 pm
by JPlush76
lol Takuma, I hope he has auto-login checked off :)

Posted: Sat Aug 24, 2002 2:50 pm
by hob_goblin
cookies

Posted: Sat Aug 24, 2002 3:15 pm
by JPlush76
cake

Posted: Sat Aug 24, 2002 4:00 pm
by Takuma
What you talking about hot_goblin and JPlush76?

Pie...

Posted: Sat Aug 24, 2002 4:01 pm
by JPlush76
mmmm apple pie with cake and cookies

the best of all worlds :twisted:

Posted: Sat Aug 24, 2002 4:22 pm
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