PHP page layout
Posted: Wed Sep 07, 2005 7:19 pm
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:
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:
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
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>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();
?>--
Edwardaux