PHP page layout

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

Post Reply
edwardaux
Forum Newbie
Posts: 6
Joined: Thu Sep 01, 2005 5:55 pm

PHP page layout

Post by edwardaux »

Lets say that, in general, I want all my pages to have a header, left menu, right menu, and body... to do this, I might do something along the lines of:

Code: Select all

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>My page</title>
<link rel="STYLESHEET" type="text/css" href="/style.css"/>
</head>
<body>

<?php
include "common/include/banner.php";
include "common/include/leftMenu.php";
include "common/include/rightMenu.php";
?>

<div id="body">
blah blah blah
</div>

</body>
</html>
But there is still a lot of stuff that I have to copy/paste into *every* page. For example, if I want to change the name of the stylesheet, I have to change every single page; likewise, if I wanted to add a footer I would have to modify every page. Is there a typical way to solve this problem?

I thought of putting all that header stuff (ie. everything before the body div) into another include file and having each page include that, but this will only work if every page had the same banner/leftMenu/rightMenu. For me, the commonality is in the layout - not in the specific contained components.

Struts (a Java framework) has a neat extension called Tiles that helps manage this very problem... has anyone seen a similar technique for PHP? I've been thinking about developing a Struts-like library something along the lines of:

Code: Select all

<?php
require_once "myGroovyStrutsLookalike.php";

startPage("/layout.php");
setComponent("banner", "common/include/banner.php");
setComponent("left", "common/include/leftMenu.php");
setComponent("right", "common/include/rightMenu.php");
?>

<div id="body">
blah blah blah
</div>

<?php
endPage();
?>
Where those functions I'm calling would read a template (layout.php) and magically import the referenced files into the right spot. That way each page can have a common layout, yet still plug in their own specific versions of the menus, etc. Are there already libraries out there that do this or something similar? Many thanks... sorry for the long post!

--
Edwardaux
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

We have an active discussion on this :arrow: viewtopic.php?t=37775
edwardaux
Forum Newbie
Posts: 6
Joined: Thu Sep 01, 2005 5:55 pm

Post by edwardaux »

Indeed you do. My apologies... I didn't think to search in that forum. I will head over there and see if I can contribute.

--
Edwardaux
Post Reply