Master pages in PHP

One of the good things about  .NET is that you can you use master pages. A master page is basically a template page which you can use to control the layout of other pages. Its usefull to ensure consistency across all pages on a site and most importantly it cuts down on a lot of repetitive programming.

PHP does not support such a feature but its easy enough to achieve the same effect. The way I do this is by creating a PHP page called master.php. I fill out the HTML parts as normal and in the place which I want to have varying content between pages, I add PHP variables.

When implementing the inheriting pages later I define the same variables but also assign content to them. After the variables have been assigned content html, I import my master.php page.  Nice.

Here is an example:

The master page, Master.php




<div id=”middle”>
<div id=”middleLeft”><?php echo $text; ?></div>



An a content page, about.php:

<?php ….

$text = “This is a good example”

include(“master.php”); ?>

2 thoughts on “Master pages in PHP

Leave a Reply

Your email address will not be published. Required fields are marked *