PHP


Creating a Basic Template System in PHP
By Richard Clark
01-Nov-09
Views: 6069

This article shows you how to use the php include function to implement a template system in PHP. It will not be a full fledged template system, it will rather teach you how to setup a basic template system in PHP
 
Creating a Basic Template System in PHP (Page 1 of 1)
After implmenting this template system in your webistes, you will achieve the following benefits
  1. If you have a lot of pages that use the same design. If you want to make a layout change in the header, menu, footer or right section then you don't have to update numerous files. You will have to edit a single php file
  2. You will have less vertical scrolling when typing PHP code as the whole section will be replaced by a single line of php (i.e we will use php include function to include an external file into the main script and the number of lines in the main script will be reduced).
  3. You will have a consistent look and feel across the website.
  4. Designers can modify different sections without intervention from the programmer.

 

Before you start, you can Download files in this tutorial

Now lets start creating our own custom template system. So what is the technique ??? The idea is to create separate files for sections that use the same design. Then include these sections in our main page. In this tutorial i haved created 4 sections i.e header, top-bar, left menu and the footer. I have created separate files for these sections and then included these sections in main pages.

 

Template System in PHP, Preview

 

See the sample code of my home page below

<?
	/* This file wil declare all the configuration varibles, we
	 * have only 1 variable in it to store appliation title
	*/
 
	include("includes/config.php");
 
	/* All the pages will have this varible at the top 
	 * to store the title of every page. This varible will be
	 * used in the title tag of HTML and can also be used elsewhere
	 */
	$page_title='Home';
 ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link href="css/style.css" type="text/css" rel="stylesheet" />
<title><?=APP_TITLE ?> - <?=$page_title ?></title>
</head>

<body>
	<div id="container">
    	<div id="header"><? include("includes/header.php"); ?></div>
    	<div id="topBar"><? include("includes/top-bar.php"); ?></div>
        <div id="menu"><? include("includes/menu.php"); ?></div>
        <div id="content">
        	<h1>The main content will go here ...</h1>
        </div>
        <br style="clear:both;" />
        <div id="footer"><? include("includes/footer.php"); ?> </div>
	</div>
</body>
</html>

 

The included file can be HTML files but sometimes you also need to write PHP code in sections, so i have saved these files as PHP. The code for individual files is given below

 

Config

<?
	define("APP_TITLE","Kings Hospital [Admin Panel]");
 ?>

 

Header

<div>
	<h1 style="color:#F7F7F7;"><?=APP_TITLE ?> - <?=$page_title ?> </h1>
</div>

 

Top Bar

<div style="background-color:#dadada;border-bottom:solid 1px #BBB; text-align:right; padding:5px;">
	Logged in as Administrator <a href="#">Logout</a>
</div>

 

Left Menu

<div>
	<ul>
    	<li><a href="index.php">Home</a></li>
    	<li><a href="employees.php">Doctors</a></li>
    	<li><a href="employees.php">Employees</a></li>
    	<li><a href="departments.php">Departments</a></li>
    	<li><a href="groups.php">Appointments</a></li>
    	<li><a href="locations.php">Settings</a></li>
    </ul>
</div>

 

Footer

<div style="border-top:dotted 1px #CCC; padding:20px;">
Copyright All rights reserved
</div>

 

CSS


body{
	margin:0;
	font-family:Verdana, Geneva, sans-serif;
	font-size:12px;
}
#container{
	width:1004px;
	margin:0px auto;
	border:solid 1px #CCC;
	border-top:none;
}
h1{
	font-family:Georgia, "Times New Roman", Times, serif;
	margin:0px;
	padding:5px 0px 15px 15px;
	font-size:22px;
}
#header{
	background-color:#26469B;
	padding:20px 0px 0px 30px;
}
#topBar{
	background:#F8F8F8;
}
#menu{
	font-family:"Trebuchet MS", Arial, Helvetica, sans-serif;
	font-size:12px;
	width:180px;
	margin:0px;
	padding:20px;
	border-right:dotted 1px #CCC;
	float:left;
}
#content{
	float:left;
	width:742px;
	padding:20px;
	padding-bottom:0px;
}
#footer{
	font-size:11px;
}

 

The last thing is how will you create other pages? Very simple, just save-as this page & add content in the content div and change the title of the page at the top.
Comments
Aprico
[17-Jan-2010]
#1

Great technique, a ready made template to start with. Thank you!

blackbird
[17-Feb-2010]
#2

this is wonderful tutorial .. i read it 5 times and get a fantastic results and sure i put a copy of this lesson on my site here theblackbird.3web.me/vb thanks fir you

OldKage
[22-Feb-2010]
#3

Thanks for the tutorial!.. This is very helpful. I've been looking for this kind of tut for quite some time now, all i found was basic html/css templating. Now this is a new one!..

SuperMario290
[15-Mar-2010]
#4

Great tutorial, very useful, but using inline styles is kinda a no-no. you're supposed to separate content from design, in using inline styles, you're not doing as such. 

Joe S. Watson
[21-Mar-2010]
#5

I am very new to the concept of php, and while generally speaking I do not find the concepts of scripting or programming all that difficult to understand I will admit that it isn’t something I am going to be able to master in a holiday weekend.

My question os so basic and so fundamentally simple that I a sure you are going to laugh until you fall off your chair over this but I would not be asking if  it wasn’t driving me crazy, having difficulties getting my simple scripts to run with out error messages all over the place

So this is what I don’t understand.
None of the books I read, none of the tutorials, and none of the examples I have looked at every say anything about including the html “doctype” in  my php documents?
This is especially driving me to tears when trying to deal with things like cookies or sessions, or even variables if I am understanding what I read the right way, because it is not clear weather those codes go between the doctype declaration  and the HTML tags, or before the doctype declaration .
Or for that mater is the html doctype declaration even necessary in php documents at all???
And if it is, does it matter weather I use transitional html 4.01or strict xhtml 1.0 

So do I do it like this????

<?php

... PHP code ...
like cookies, sessions, & variables

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <title></title>
</head>
<body>

</body>
</html>

Or, Like this????

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<?php

... PHP code ...
like cookies, sessions, & variables

?>

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <title></title><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <title></title>
</head>
<body>

</body>
</html>
</head>
<body>

Or like this????

<?php

... PHP code ...
like cookies, sessions, & variables

?>

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <title></title>
</head>
<body>

</body>
</html>


jaredborit
[26-Mar-2010]
#6

thank you for  the template.....it would be a big help for me in doing my project

Zach Lysobey
[08-Jul-2010]
#7

@Joe : I am anything but a pro myself, but I do know a thing or 2 about how all this works; and I think you are way overthinking it.  The php code will all be read by the server, which ignores all the regular html (including doctype etc...) When the server passes the resulting code to the browser, it no longer includes the php code,its just HTML.  

I belive that it is only important that all your cookies sessions variables and whatever go above php elements that need them.... i.e.  it doesnt really matter what order all that stuff goes in at the top.

Hope that helps

Leave a Comment
Age (Required, will not be shown)
Name
Email (Required, will not be shown)
Website (Optional, starting with http://)
 
Are you human ?

Enter the code shown above