Page 1 of 1

PHP page layout

Posted: Wed Sep 07, 2005 7:19 pm
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

Posted: Wed Sep 07, 2005 7:21 pm
by John Cartwright
We have an active discussion on this :arrow: viewtopic.php?t=37775

Posted: Wed Sep 07, 2005 7:44 pm
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