Basics
Creating a Document
Head and Body Elements
Formatting Text
Creating Lists
Embedding Images
URLs Explained
Linking to Documents
Creating Tables
Forms Form Basics
Input and Textarea
Select Lists
Frames Framesets
The Frame Element
Nested Framesets
Targetting Frames
Inline Frames
Tips and Tricks Meta Tags
Transition Effects
HTML Generators Create a Document
Create a List
Create a Table
HTML
Basics
Implementing CSS
CSS Syntax
Pseudo Classes/Elements
CSS Classes
CSS Properties Font Properties
Color and Background
Text Properties
Border Properties
Margins and Padding
Size and Position
Tips and Tricks Menu Buttons
Special Effects
CSS
Basics
Running a Script
Variables
Expressions and Operators
Objects.Properties.Methods
The Date Object
Strings
Regular Expressions
Defining RegExp Patterns
Branches and Conditions
Loops
Arrays Array Basics
Array Methods
Sorting Arrays

User-Defined Functions
Cookies
Windows
Frames
Tips and Tricks Image Replacement
Using Includes
Form Validation
Debugging
JavaScript
Basics
Creating a Script
Running a Script
Variables
Expressions and Operators
Strings Strings Basics
Strings and Substrings
Replacing Substrings
Regular Expressions
Branches and Conditions
Loops
Arrays Array Basics
Array Functions
Sorting Arrays
User-Defined Functions
Include and Require
Uploading Files
File Functions
Session Variables
Tips and Tricks Page Templates
Form Reply Scripts
Form Validation
JavaScript to PHP
PHP
Basics
Create and Drop
Show and Describe
Insert, Update and Delete
Querying
Join Queries
Functions
Table Locking
PHP/MySQL Functions Accessing a Database
Querying with PHP
Create and Drop with PHP
Insert and Update with PHP
Frequently Used Functions MySQL
Basics
Layout and Navigation
Page Content Style
Web Page Copy
Graphics and Animation
HTML Forms
Accessibility
Legal Requirements
MySQL
PHPBasicsCreating a ScriptRunning a ScriptVariablesExpressions and OperatorsStringsStrings and SubstringsReplacing SubstringsRegular ExpressionsBranches and ConditionsLoopsArraysArray FunctionsSorting ArraysUser-Defined FunctionsInclude and RequireUploading FilesFile FunctionsSession Variables
Recommended Reading
PHP and MySQL web development

programming PHP

PHP cookbook

Home > PHP Tips and Tricks > Page Templatesprinter version

PHP Page Templates

The PHP include() statement is useful for creating page templates.

If sections of pages that are common to all pages are placed within include() files then this cuts down on site maintenance.

Any change only needs to made to the include() file without having to change every page on the site.

A Typical Two Column Layout

On the example layout below the only variations between the pages on the site will be the HTML metatags within the head section and the content of the page.

PHP page template layout


We can create a header section that will include the head section of the page and the logo, a navigation section to hold the page links and a footer section.

These three sections can be created as separate files and included within all pages of the site.

A Typical Page with include() Statements
<?php
$title = 'My title';
$description = 'My description';
$keywords = 'My keywords';
include('header.php');
?>
<table width="100%"><tr><td width="20%" valign="top">
<?php
$index = true;
include('navigation.php');
?>
</td><td width="80%" valign="top">
Page content here
</td></tr></table>
<?php
include('footer.php');
?>


Before including the header we can set the individual metatags for each particular page by using the variables $title, $description and $keywords.

Before including the navigation we can set a variable to signal the page name to the navigation menu. This is to avoid having a page that links to itself.

A Typical Header Section
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title><?php echo($title);?></title>
<meta name="<?php echo($description);?>">
<meta name="keywords" content="<?php echo($keywords);?>">
</head>
<body>
<img src="images/logo.gif" width="242" height="48" alt="logo">


In the header above we could alter the logo across the whole site with only one change to the included file.

A Typical Navigation Section
<?php
if ($index)
{
    $index = 'Home';
}else{
    $index = '<a href="index.php">Home</a>';
}
if ($services)
{
    $services = 'Services';
}else{
    $services = '<a href="services.php">Services</a>';
}
if ($contact_us)
{
    $contact_us = 'Contact Us';
}else{
    $contact_us = '<a href="contact_us.php">Contact Us</a>';
}
echo($index.'<br>'.$services.'<br>'.$contact_us);
?>


The code displays the page name as plain text instead of a link if that's the page the user is on.

If we need to add an additional page to the site then it only needs to be added to the included navigation file.

A Typical Footer Section
<center>
<?php
echo('©'.date(Y).' www.mydomain.com all rights reserved');
?>
</center>
</body>
</html>


Finally the footer section finishes off the page and includes the PHP date function to return the current year.


Privacy | Terms | Contact | Links | Sitemap | RSS Feeds RSS and JavaScript Feeds
©2010 www.webdesignworkmate.co.uk all rights reserved 
Design and Production by smallbizonline website design © 2000-2010
Valid HTML 4.01! Level Double-A conformance icon, W3C-WAI Web Content Accessibility Guidelines 1.0Valid CSS!
Tips and TricksPHP page templatesPage TemplatesForm Reply ScriptsForm ValidationJavaScript to PHP
Got any PHP Tips?
Send me your tip and if it's suitable I'll put it on the site, credit it to you and add a link back to your site.