PHP inlcudes() template theory

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
BelizeNut
Forum Newbie
Posts: 4
Joined: Tue May 12, 2009 7:12 pm

PHP inlcudes() template theory

Post by BelizeNut »

Hi everyone! I am new to PHP and am looking for some advice with my overall website template construct.

Right now I have a master template (aka my homepage, index.php) using includes() with header.php, navbar.php, left_col.php, right_col.php, and footer.php. (pretty standard i think).

index.php is in the root folder
header, navbar, left_col, right_col, and footer are in [root]/layout folder.
All my subpages are in the [root]/pages folder.
header, navbar, left_col, and footer will be staying the same on each page (well, left_col might change).
I basically wanna change out right_col for the different pages.

I understand I can just copy all the code in index.php into the different pages and the template will look the same... BUT, here's my question, is there an easier way to accomplish this (not that this is so hard)? Like, for instance if my template changes to three columns in the future, I would have to go into each subpage and update the general template!!! Is there anyway around this, like somehow being able to just call index.php (the master template) in my subpages and switch out right_col with the appropriate content?

Thanks in advance for all your replies!

Cheers,
Matt
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: PHP inlcudes() template theory

Post by alex.barylski »

To answer your question, yes, there is a better way. What you are doing is typically refered to as a pull template approach, where each page typically pulls on a header, footer include to generate the page. This works well, for small sites that have simple layouts that rarely change. I just recently started on a project that probably has over 100 scripts built this way. The problem with this approach is that sooner or later someone gets lazy and begins putting layout code in individual scripts, and duplicaiton occurs and bam you have yourself a wicked mess.

The prefer approach is to use a "push" template approach where the template is the general layout and you push the unique content into placeholders, which are located wherever you wish inside the master template.

Google Smarty for a more detailed explanation of how push templating works.

Cheers,
Alex
BelizeNut
Forum Newbie
Posts: 4
Joined: Tue May 12, 2009 7:12 pm

Re: PHP inlcudes() template theory

Post by BelizeNut »

Alex! You are the man!

PHP seems so powerful and flexible that I knew there must be a way. I just couldn't see myself updating 100+ pages when the template changed even by a little.

Thank you so much.

Cheers,
Matt
Post Reply