Let's Know about Web Designing

How to Create a PHP Website Template from Scratch

Posted on: April 21, 2010

This is a tutorial on creating a PHP website template starting with HTML and CSS. We will start with the basics and you can also download the final product. Please remember that I am using very basic CSS styling in this example just for you to get the idea, and not so much to make it look pretty. The download will contain both the styled example as well as a complete blank template that you can use for your own starting point for any project personal or commercial. The demo files are released under GPL V2.

This tutorial assumes you have basic understanding of html and css. At the end of this tutorial you should have a basic understanding of using php and converting an html site to php.You can also download the demo files here.

The actual template will be created in 10 easy steps. I will then take it a step further to show you how to add variables to your template.

Setting up the File structure and Folders

Step One

Let’s start by creating a new folder. I have named this folder php_site

Inside of this folder we are now going to create two new files. One is index.html and the other file is going to be named style.css

Step Two

Now we are going to create two more folders inside of our main folder. The first folder is going to be named includes, and the second folder will be named variables.

We should now have a setup that looks like the following:

Step Three

Now, using your favorite html editor open the index.html file. We are going to to create a basic html website document. This is the code I am using:

01 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
02
03 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
04
05 <head>
06
07 <meta http-equiv="content-type" content="text/html; charset=utf-8" />
08
09 <meta name="description" content="" />
10
11 <meta name="keywords" content="" />
12
13 <meta name="author" content="" />
14
15 <link rel="stylesheet" type="text/css" href="style.css" media="screen" />
16
17 <title>1stWebDesigner PHP Template</title>
18
19 </head>
20
21 <body>
22
23 <div id="wrapper">
24
25 <div id="header">
26
27 </div> <!-- end #header -->
28
29 <div id="nav">
30
31 </div> <!-- end #nav -->
32
33 <div id="content">
34
35 </div> <!-- end #content -->
36
37 <div id="sidebar">
38
39 </div> <!-- end #sidebar -->
40
41 <div id="footer">
42
43 </div> <!-- end #footer -->
44
45 </div> <!-- End #wrapper -->
46
47 </body>
48
49 </html>

Step Four

Now I am going to open up the style.css file and add the divs we will be using. I have added very basic styling so you can see the site and get the idea of what we are doing. Remember that you can use your own styling when creating your website to suit your needs.

This is the CSS I am adding to the stylesheet:

01 body {
02 background-color:#f1f1f1;
03 font-family: georgia,sans-serif;
04 color:#333;
05 margin:0;
06 padding:0;
07 }
08
09 #wrapper {
10 width:960px;
11 background-color:#f8f8f8;
12 margin:0 auto;
13 border-left:1px solid #ccc;
14 border-right:1px solid #ccc;
15 }
16
17 #header {
18 width:960px;
19 height:135px;
20 margin:0 auto;
21 margin-bottom:25px;
22 border-bottom:1px solid #ccc;
23 border-top:1px solid #ccc;
24 }
25
26 #header h2 {
27 padding:10px;
28 }
29
30 #nav {
31 width:960px;
32 height:40px;
33 border-bottom:1px solid #ccc;
34 }
35
36 #nav a {
37 display:inline;
38 padding:10px;
39 text-decoration:none;
40 background-color:#f1f1f1;
41 }
42
43 #nav a:hover {
44 background-color:#bababa;
45 height:80px;
46 }
47
48 #content {
49 width:675px;
50 float:left;
51 padding:10px;
52 }
53
54 #sidebar {
55 width:200px;
56 float:right;
57 margin-bottom:25px;
58 }
59
60 #sidebar a {
61 text-decoration:none;
62 }
63
64 #sidebar li {
65 list-style:none;
66 }
67
68 #footer {
69 clear:both;
70 width:960px;
71 height:135px;
72 border-top:1px solid #ccc;
73 }
74
75 #footer p {
76 padding:10px;
77 }

Step Five

Next we will create some more files that we will be using when we start converting our template to php.

Open up the folder we created inside of our main folder called variables. Next, create a blank file called variables.php

Then go into the other folder we created called includes and create the following files:

header.php
nav.php
sidebar.php
footer.php

Step Six

Now we will need to add some content to our index.html file. I have filled in the header area with an H2 tag for the website header title. I will also be adding the links in the nav area, as well as some text in the content area, sidebar, and footer. The final index.html file now looks like this:

001 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
002
003 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
004
005 <head>
006
007 <meta http-equiv="content-type" content="text/html; charset=utf-8" />
008
009 <meta name="description" content="" />
010
011 <meta name="keywords" content="" />
012
013 <meta name="author" content="" />
014
015 <link rel="stylesheet" type="text/css" href="style.css" media="screen" />
016
017 <title>1stWebDesigner PHP Template</title>
018
019 </head>
020
021 <body>
022
023 <div id="wrapper">
024
025 <div id="header">
026
027 <h2>1stWebDesigner PHP Template</h2>
028
029 </div> <!-- end #header -->
030
031 <div id="nav">
032
033 <a href="#">Home</a>
034 <a href="#">About</a>
035 <a href="#">Portfolio</a>
036 <a href="#">Contact</a>
037
038 </div> <!-- end #nav -->
039
040 <div id="content">
041
042 <h1>Heading1</h1>
043 <h2>Heading2</h2>
044 <h3>Heading3</h3>
045 <h4>Heading4</h4>
046 <h5>Heading5</h5>
047
048 <h3>Paragraph Element</h3>
049
050 <p>
051
052 Quisque pellentesque sodales aliquam. Morbi mollis neque eget arcu egestas non ultrices neque volutpat. Nam at nunc lectus, id vulputate purus. In et turpis ac mauris viverra iaculis. Cras sed elit a purus ultrices iaculis eget sit amet dolor. Praesent ac libero dolor, id viverra libero. Mauris aliquam nibh vitae eros sodales fermentum. Fusce cursus est varius ante vehicula eget ultrices felis eleifend. Nunc pharetra rutrum nibh et lobortis. Morbi vitae venenatis velit.
053
054 </p>
055
056 <p>
057
058 Quisque pellentesque sodales aliquam. Morbi mollis neque eget arcu egestas non ultrices neque volutpat. Nam at nunc lectus, id vulputate purus. In et turpis ac mauris viverra iaculis. Cras sed elit a purus ultrices iaculis eget sit amet dolor. Praesent ac libero dolor, id viverra libero. Mauris aliquam nibh vitae eros sodales fermentum. Fusce cursus est varius ante vehicula eget ultrices felis eleifend. Nunc pharetra rutrum nibh et lobortis. Morbi vitae venenatis velit.
059
060 </p>
061
062 <h3>Another Heading Starting Point</h3>
063
064 <p>
065
066 Quisque pellentesque sodales aliquam. Morbi mollis neque eget arcu egestas non ultrices neque volutpat. Nam at nunc lectus, id vulputate purus. In et turpis ac mauris viverra iaculis. Cras sed elit a purus ultrices iaculis eget sit amet dolor. Praesent ac libero dolor, id viverra libero. Mauris aliquam nibh vitae eros sodales fermentum. Fusce cursus est varius ante vehicula eget ultrices felis eleifend. Nunc pharetra rutrum nibh et lobortis. Morbi vitae venenatis velit.
067
068 </p>
069
070 <p>
071
072 Quisque pellentesque sodales aliquam. Morbi mollis neque eget arcu egestas non ultrices neque volutpat. Nam at nunc lectus, id vulputate purus. In et turpis ac mauris viverra iaculis. Cras sed elit a purus ultrices iaculis eget sit amet dolor. Praesent ac libero dolor, id viverra libero. Mauris aliquam nibh vitae eros sodales fermentum. Fusce cursus est varius ante vehicula eget ultrices felis eleifend. Nunc pharetra rutrum nibh et lobortis. Morbi vitae venenatis velit.
073
074 </p>
075
076 </div> <!-- end #content -->
077
078 <div id="sidebar">
079
080 <h3>Navigation</h3>
081 <li><a href="#">Home</a></li>
082 <li><a href="#">About Us</a></li>
083 <li><a href="#">Links</a></li>
084 <li><a href="#">Portfolio</a></li>
085 <li><a href="#">Contact</a></li>
086
087 <h3>Box Two</h3>
088 <li><a href="#">Link Here</a></li>
089 <li><a href="#">Link Here</a></li>
090 <li><a href="#">Link Here</a></li>
091 <li><a href="#">Link Here</a></li>
092 <li><a href="#">Link Here</a></li>
093
094 <h3>Box Three</h3>
095 <li><a href="#">Link Here</a></li>
096 <li><a href="#">Link Here</a></li>
097 <li><a href="#">Link Here</a></li>
098 <li><a href="#">Link Here</a></li>
099 <li><a href="#">Link Here</a></li>
100
101 </div> <!-- end #sidebar -->
102
103 <div id="footer">
104 <p>Copyright &copy 2010 <a href="http://www.1stwebdesigner.com">1stWebDesigner.com PHP Template</a></p>
105 </div> <!-- end #footer -->
106
107 </div> <!-- End #wrapper -->
108
109 </body>
110
111 </html>

Step Seven

Now we will start converting our template to PHP. We will need to take a brief look at the html code we have and decide which areas will be sliced. We will be removing specific areas from the index.html file and placing them into the php files we have created inside the includes folder.

Let’s start with the top of the html document on the header. Copy the header div and cut it from the document. Then open your includes folder, and the header.php file we created and add the code to the header.php

header.php

1 <div id="header">
2
3 <h2>1stWebDesigner PHP Template</h2>
4
5 </div> <!-- end #header -->

Make sure this area has now been removed completely from the index.html file and placed into the header.php file. In place of this code, we will now add the following to the index.html file.

1 <?php include('includes/header.php'); ?>

Now our index.html should look like this: (Make a note that the header area has now been replaced with our php include function that is calling our external header.php file)

001 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
002
003 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
004
005 <head>
006
007 <meta http-equiv="content-type" content="text/html; charset=utf-8" />
008
009 <meta name="description" content="" />
010
011 <meta name="keywords" content="" />
012
013 <meta name="author" content="" />
014
015 <link rel="stylesheet" type="text/css" href="style.css" media="screen" />
016
017 <title>1stWebDesigner PHP Template</title>
018
019 </head>
020
021 <body>
022
023 <div id="wrapper">
024
025 <?php include('includes/header.php'); ?>
026
027 <div id="nav">
028
029 <a href="#">Home</a>
030 <a href="#">About</a>
031 <a href="#">Portfolio</a>
032 <a href="#">Contact</a>
033
034 </div> <!-- end #nav -->
035
036 <div id="content">
037
038 <h1>Heading1</h1>
039 <h2>Heading2</h2>
040 <h3>Heading3</h3>
041 <h4>Heading4</h4>
042 <h5>Heading5</h5>
043
044 <h3>Paragraph Element</h3>
045
046 <p>
047
048 Quisque pellentesque sodales aliquam. Morbi mollis neque eget arcu egestas non ultrices neque volutpat. Nam at nunc lectus, id vulputate purus. In et turpis ac mauris viverra iaculis. Cras sed elit a purus ultrices iaculis eget sit amet dolor. Praesent ac libero dolor, id viverra libero. Mauris aliquam nibh vitae eros sodales fermentum. Fusce cursus est varius ante vehicula eget ultrices felis eleifend. Nunc pharetra rutrum nibh et lobortis. Morbi vitae venenatis velit.
049
050 </p>
051
052 <p>
053
054 Quisque pellentesque sodales aliquam. Morbi mollis neque eget arcu egestas non ultrices neque volutpat. Nam at nunc lectus, id vulputate purus. In et turpis ac mauris viverra iaculis. Cras sed elit a purus ultrices iaculis eget sit amet dolor. Praesent ac libero dolor, id viverra libero. Mauris aliquam nibh vitae eros sodales fermentum. Fusce cursus est varius ante vehicula eget ultrices felis eleifend. Nunc pharetra rutrum nibh et lobortis. Morbi vitae venenatis velit.
055
056 </p>
057
058 <h3>Another Heading Starting Point</h3>
059
060 <p>
061
062 Quisque pellentesque sodales aliquam. Morbi mollis neque eget arcu egestas non ultrices neque volutpat. Nam at nunc lectus, id vulputate purus. In et turpis ac mauris viverra iaculis. Cras sed elit a purus ultrices iaculis eget sit amet dolor. Praesent ac libero dolor, id viverra libero. Mauris aliquam nibh vitae eros sodales fermentum. Fusce cursus est varius ante vehicula eget ultrices felis eleifend. Nunc pharetra rutrum nibh et lobortis. Morbi vitae venenatis velit.
063
064 </p>
065
066 <p>
067
068 Quisque pellentesque sodales aliquam. Morbi mollis neque eget arcu egestas non ultrices neque volutpat. Nam at nunc lectus, id vulputate purus. In et turpis ac mauris viverra iaculis. Cras sed elit a purus ultrices iaculis eget sit amet dolor. Praesent ac libero dolor, id viverra libero. Mauris aliquam nibh vitae eros sodales fermentum. Fusce cursus est varius ante vehicula eget ultrices felis eleifend. Nunc pharetra rutrum nibh et lobortis. Morbi vitae venenatis velit.
069
070 </p>
071
072 </div> <!-- end #content -->
073
074 <div id="sidebar">
075
076 <h3>Navigation</h3>
077 <li><a href="#">Home</a></li>
078 <li><a href="#">About Us</a></li>
079 <li><a href="#">Links</a></li>
080 <li><a href="#">Portfolio</a></li>
081 <li><a href="#">Contact</a></li>
082
083 <h3>Box Two</h3>
084 <li><a href="#">Link Here</a></li>
085 <li><a href="#">Link Here</a></li>
086 <li><a href="#">Link Here</a></li>
087 <li><a href="#">Link Here</a></li>
088 <li><a href="#">Link Here</a></li>
089
090 <h3>Box Three</h3>
091 <li><a href="#">Link Here</a></li>
092 <li><a href="#">Link Here</a></li>
093 <li><a href="#">Link Here</a></li>
094 <li><a href="#">Link Here</a></li>
095 <li><a href="#">Link Here</a></li>
096
097 </div> <!-- end #sidebar -->
098
099 <div id="footer">
100 <p>Copyright &copy 2010 <a href="#">Website Name</a></p>
101 </div> <!-- end #footer -->
102
103 </div> <!-- End #wrapper -->
104
105 </body>
106
107 </html>

Step Eight

Now we will continue to slice the html document and place the code in the proper php files we made.

Next let’s move to the nav div. The same as above, we are going to copy and cut that piece of code and place it into our nav.php file that is located inside of the includes folder.

This is what we are adding to the nav.php file:

1 <div id="nav">
2
3 <a href="#">Home</a>
4 <a href="#">About</a>
5 <a href="#">Portfolio</a>
6 <a href="#">Contact</a>
7
8 </div> <!-- end #nav -->

Again we will replace the code removed from the html document with the following:

1 <?php include(includes/nav.php); ?>

Now our index.html document will look like this: (Again note that both the header, and the nav area are now replaced with php includes)

001 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
002
003 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
004
005 <head>
006
007 <meta http-equiv="content-type" content="text/html; charset=utf-8" />
008
009 <meta name="description" content="" />
010
011 <meta name="keywords" content="" />
012
013 <meta name="author" content="" />
014
015 <link rel="stylesheet" type="text/css" href="style.css" media="screen" />
016
017 <title>1stWebDesigner PHP Template</title>
018
019 </head>
020
021 <body>
022
023 <div id="wrapper">
024
025 <?php include('includes/header.php'); ?>
026
027 <?php include('includes/nav.php'); ?>
028
029 <div id="content">
030
031 <h1>Heading1</h1>
032 <h2>Heading2</h2>
033 <h3>Heading3</h3>
034 <h4>Heading4</h4>
035 <h5>Heading5</h5>
036
037 <h3>Paragraph Element</h3>
038
039 <p>
040
041 Quisque pellentesque sodales aliquam. Morbi mollis neque eget arcu egestas non ultrices neque volutpat. Nam at nunc lectus, id vulputate purus. In et turpis ac mauris viverra iaculis. Cras sed elit a purus ultrices iaculis eget sit amet dolor. Praesent ac libero dolor, id viverra libero. Mauris aliquam nibh vitae eros sodales fermentum. Fusce cursus est varius ante vehicula eget ultrices felis eleifend. Nunc pharetra rutrum nibh et lobortis. Morbi vitae venenatis velit.
042
043 </p>
044
045 <p>
046
047 Quisque pellentesque sodales aliquam. Morbi mollis neque eget arcu egestas non ultrices neque volutpat. Nam at nunc lectus, id vulputate purus. In et turpis ac mauris viverra iaculis. Cras sed elit a purus ultrices iaculis eget sit amet dolor. Praesent ac libero dolor, id viverra libero. Mauris aliquam nibh vitae eros sodales fermentum. Fusce cursus est varius ante vehicula eget ultrices felis eleifend. Nunc pharetra rutrum nibh et lobortis. Morbi vitae venenatis velit.
048
049 </p>
050
051 <h3>Another Heading Starting Point</h3>
052
053 <p>
054
055 Quisque pellentesque sodales aliquam. Morbi mollis neque eget arcu egestas non ultrices neque volutpat. Nam at nunc lectus, id vulputate purus. In et turpis ac mauris viverra iaculis. Cras sed elit a purus ultrices iaculis eget sit amet dolor. Praesent ac libero dolor, id viverra libero. Mauris aliquam nibh vitae eros sodales fermentum. Fusce cursus est varius ante vehicula eget ultrices felis eleifend. Nunc pharetra rutrum nibh et lobortis. Morbi vitae venenatis velit.
056
057 </p>
058
059 <p>
060
061 Quisque pellentesque sodales aliquam. Morbi mollis neque eget arcu egestas non ultrices neque volutpat. Nam at nunc lectus, id vulputate purus. In et turpis ac mauris viverra iaculis. Cras sed elit a purus ultrices iaculis eget sit amet dolor. Praesent ac libero dolor, id viverra libero. Mauris aliquam nibh vitae eros sodales fermentum. Fusce cursus est varius ante vehicula eget ultrices felis eleifend. Nunc pharetra rutrum nibh et lobortis. Morbi vitae venenatis velit.
062
063 </p>
064
065 </div> <!-- end #content -->
066
067 <div id="sidebar">
068
069 <h3>Navigation</h3>
070 <li><a href="#">Home</a></li>
071 <li><a href="#">About Us</a></li>
072 <li><a href="#">Links</a></li>
073 <li><a href="#">Portfolio</a></li>
074 <li><a href="#">Contact</a></li>
075
076 <h3>Box Two</h3>
077 <li><a href="#">Link Here</a></li>
078 <li><a href="#">Link Here</a></li>
079 <li><a href="#">Link Here</a></li>
080 <li><a href="#">Link Here</a></li>
081 <li><a href="#">Link Here</a></li>
082
083 <h3>Box Three</h3>
084 <li><a href="#">Link Here</a></li>
085 <li><a href="#">Link Here</a></li>
086 <li><a href="#">Link Here</a></li>
087 <li><a href="#">Link Here</a></li>
088 <li><a href="#">Link Here</a></li>
089
090 </div> <!-- end #sidebar -->
091
092 <div id="footer">
093 <p>Copyright &copy 2010 <a href="#">Website Name</a></p>
094 </div> <!-- end #footer -->
095
096 </div> <!-- End #wrapper -->
097
098 </body>
099
100 </html>

Step Nine

Now it is time to do the sidebar. Copy and cut the sidebar div and place it into the sidebar.php file we created that is located inside the includes folder.

This is the code we are adding:

sidebar.php

01 <div id="sidebar">
02
03 <h3>Navigation</h3>
04 <li><a href="#">Home</a></li>
05 <li><a href="#">About Us</a></li>
06 <li><a href="#">Links</a></li>
07 <li><a href="#">Portfolio</a></li>
08 <li><a href="#">Contact</a></li>
09
10 <h3>Box Two</h3>
11 <li><a href="#">Link Here</a></li>
12 <li><a href="#">Link Here</a></li>
13 <li><a href="#">Link Here</a></li>
14 <li><a href="#">Link Here</a></li>
15 <li><a href="#">Link Here</a></li>
16
17 <h3>Box Three</h3>
18 <li><a href="#">Link Here</a></li>
19 <li><a href="#">Link Here</a></li>
20 <li><a href="#">Link Here</a></li>
21 <li><a href="#">Link Here</a></li>
22 <li><a href="#">Link Here</a></li>
23
24 </div> <!-- end #sidebar -->

We will again do the same thing for the footer area. Copy and cut the footer div and place it into the footer.php file we created in the same includes folder.

Here is the footer.php code:

1 <div id="footer">
2 <p>Copyright &copy 2010 <a href="#">Website Name</a></p>
3 </div> <!-- end #footer -->

Now with all of our elements taken from the index.html file and added to the different php files, the final index.html should look like this:

01 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
02
03 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
04
05 <head>
06
07 <meta http-equiv="content-type" content="text/html; charset=utf-8" />
08
09 <meta name="description" content="" />
10
11 <meta name="keywords" content="" />
12
13 <meta name="author" content="" />
14
15 <link rel="stylesheet" type="text/css" href="style.css" media="screen" />
16
17 <title>1stWebDesigner PHP Template</title>
18
19 </head>
20
21 <body>
22
23 <div id="wrapper">
24
25 <?php include('includes/header.php'); ?>
26
27 <?php include('includes/nav.php'); ?>
28
29 <div id="content">
30
31 <h1>Heading1</h1>
32 <h2>Heading2</h2>
33 <h3>Heading3</h3>
34 <h4>Heading4</h4>
35 <h5>Heading5</h5>
36
37 <h3>Paragraph Element</h3>
38
39 <p>
40
41 Quisque pellentesque sodales aliquam. Morbi mollis neque eget arcu egestas non ultrices neque volutpat. Nam at nunc lectus, id vulputate purus. In et turpis ac mauris viverra iaculis. Cras sed elit a purus ultrices iaculis eget sit amet dolor. Praesent ac libero dolor, id viverra libero. Mauris aliquam nibh vitae eros sodales fermentum. Fusce cursus est varius ante vehicula eget ultrices felis eleifend. Nunc pharetra rutrum nibh et lobortis. Morbi vitae venenatis velit.
42
43 </p>
44
45 <p>
46
47 Quisque pellentesque sodales aliquam. Morbi mollis neque eget arcu egestas non ultrices neque volutpat. Nam at nunc lectus, id vulputate purus. In et turpis ac mauris viverra iaculis. Cras sed elit a purus ultrices iaculis eget sit amet dolor. Praesent ac libero dolor, id viverra libero. Mauris aliquam nibh vitae eros sodales fermentum. Fusce cursus est varius ante vehicula eget ultrices felis eleifend. Nunc pharetra rutrum nibh et lobortis. Morbi vitae venenatis velit.
48
49 </p>
50
51 <h3>Another Heading Starting Point</h3>
52
53 <p>
54
55 Quisque pellentesque sodales aliquam. Morbi mollis neque eget arcu egestas non ultrices neque volutpat. Nam at nunc lectus, id vulputate purus. In et turpis ac mauris viverra iaculis. Cras sed elit a purus ultrices iaculis eget sit amet dolor. Praesent ac libero dolor, id viverra libero. Mauris aliquam nibh vitae eros sodales fermentum. Fusce cursus est varius ante vehicula eget ultrices felis eleifend. Nunc pharetra rutrum nibh et lobortis. Morbi vitae venenatis velit.
56
57 </p>
58
59 <p>
60
61 Quisque pellentesque sodales aliquam. Morbi mollis neque eget arcu egestas non ultrices neque volutpat. Nam at nunc lectus, id vulputate purus. In et turpis ac mauris viverra iaculis. Cras sed elit a purus ultrices iaculis eget sit amet dolor. Praesent ac libero dolor, id viverra libero. Mauris aliquam nibh vitae eros sodales fermentum. Fusce cursus est varius ante vehicula eget ultrices felis eleifend. Nunc pharetra rutrum nibh et lobortis. Morbi vitae venenatis velit.
62
63 </p>
64
65 </div> <!-- end #content -->
66
67 <?php include('includes/sidebar.php'); ?>
68
69 <?php include('includes/footer.php'); ?>
70
71 </div> <!-- End #wrapper -->
72
73 </body>
74
75 </html>

Step Ten

Now that our index.html is properly sliced, we can rename the index.html file to index.php

Now that we have created this template, as long as the php includes are on any page we only have to modify the one single file to make it visible accross your entire website. This again is good for anyone with a lot of content and pages. Instead of having to edit the sidebar on every page of your site you can now just edit the sidebar.php file and it will reflect on all of your pages. This makes it a lot easier to keep up with large portions of website material.

Our folder should now look like this:

Creating PHP Variables for the template

Now we will make it a little more dynamic and even easier to be able to modify when needed. Lets take a look at the header.php file inside the includes folder.

1 <div id="header">
2
3 <h2>1stWebDesigner PHP Template</h2>
4
5 </div> <!-- end #header -->

We are going to modify this a little bit to change the H2 heading from a different file using a variable. Creating a variable is good for areas on your website that may change more than others. It may also be a good way to allow clients to edit a single file to change certain items on their website.

Now let’s replace the text within the H2 heading to this:

1 <h2><?php echo $heading ?></h2>

Now our header.php file will look like this: Notice we placed the echo php command within the H2 brackets. This will give our variable output the H2 heading we want.

1 <div id="header">
2
3 <h2><?php echo $heading ?></h2>
4
5 </div> <!-- end #header -->

Next we are going to open up the file inside of the variables folder that we created named variables.php

We will add the variable that we are going to echo on the website heading. I have named this variable heading.

variables.php

1 <?php
2
3 $heading='1stWebDesigner PHP Template';
4
5 ?>

Now, at any time we want to change what is titled on the heading of our template, we can simply open our variables.php file and modify what is between the quotes. This will always be the output on our header.php file where we placed the echo command.

One step Further

Now we will do the same thing to our footer.php file. Open the footer.php that we have in our includes folder and let’s take a look at it:

1 <div id="footer">
2 <p>Copyright &copy 2010 <a href="#">Website Name</a></p>
3 </div> <!-- end #footer -->

Let’s change this like we did the header file, and add a variable to use.

We will place our echo command within the paragraph brackets. You should now have the following in your footer.php:

1 <div id="footer">
2 <p><?php echo $footer ?></p>
3 </div> <!-- end #footer -->

And again we will visit our variables.php file located in the variables folder we made and we will now update it with our new variable. The final should now look like this:

variables.php

1 <?php
2
3 $heading='1stWebDesigner PHP Template';
4
5 $footer='Copyright &copy; 2010 1stWebDesigner PHP Template';
6
7 ?>

Make note of the above. When using PHP variables we cannot use a regular quote. They should be replaced as you see above directly after the a href=

This is to ensure the code is read properly by browsers. If you use a regular quote it will not work.

Making it all work

The last thing we need to do in order to make our variables work is to do one last php include within our template to call the variables file.

Let’s open up our header.php file and add the following:

1 <?php include('variables/variables.php'); ?>

This will tell our template to use the variables file we have created.

The header.php file should now look like this:

1 <?php include('variables/variables.php'); ?>
2
3 <div id="header">
4
5 <h2><?php echo $heading ?></h2>
6
7 </div> <!-- end #header -->

Next we will need to do the same to our footer.php file. Add the same include at the top. The footer.php should now look like this:

1 <?php include('variables/variables.php'); ?>
2
3 <div id="footer">
4 <p><?php echo $footer ?></p>
5 </div> <!-- end #footer -->

Now that these two files are calling the variables, you will see the reflected results in the final template. You can always create new variables, and create any type of php files you want. You can even change the folder directory in any way you wish as long as your php include function displays the correct path to the files.

Playing with the PHP template

To test it a bit further let’s duplicate the index.html file and make a second page that will also call the external php files. This way we will be able to see more exactly how this works.

Make a copy of your index.php file and rename the new file to about.php

Inside of this file you can change anything you want within the #content div inside of the website #wrapper

I have created the following for testing purposes:

01 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
02
03 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
04
05 <head>
06
07 <meta http-equiv="content-type" content="text/html; charset=utf-8" />
08
09 <meta name="description" content="" />
10
11 <meta name="keywords" content="" />
12
13 <meta name="author" content="" />
14
15 <link rel="stylesheet" type="text/css" href="style.css" media="screen" />
16
17 <title>1stWebDesigner PHP Template About Page</title>
18
19 </head>
20
21 <body>
22
23 <div id="wrapper">
24
25 <?php include('includes/header.php'); ?>
26
27 <?php include('includes/nav.php'); ?>
28
29 <div id="content">
30
31 <h3>About Me Page</h3>
32
33 <p>
34
35 Quisque pellentesque sodales aliquam. Morbi mollis neque eget arcu egestas non ultrices neque volutpat. Nam at nunc lectus, id vulputate purus. In et turpis ac mauris viverra iaculis. Cras sed elit a purus ultrices iaculis eget sit amet dolor. Praesent ac libero dolor, id viverra libero. Mauris aliquam nibh vitae eros sodales fermentum. Fusce cursus est varius ante vehicula eget ultrices felis eleifend. Nunc pharetra rutrum nibh et lobortis. Morbi vitae venenatis velit.
36
37 </p>
38
39 <p>
40
41 Quisque pellentesque sodales aliquam. Morbi mollis neque eget arcu egestas non ultrices neque volutpat. Nam at nunc lectus, id vulputate purus. In et turpis ac mauris viverra iaculis. Cras sed elit a purus ultrices iaculis eget sit amet dolor. Praesent ac libero dolor, id viverra libero. Mauris aliquam nibh vitae eros sodales fermentum. Fusce cursus est varius ante vehicula eget ultrices felis eleifend. Nunc pharetra rutrum nibh et lobortis. Morbi vitae venenatis velit.
42
43 </p>
44
45 <h3>Another Heading Starting Point</h3>
46
47 <p>
48
49 Quisque pellentesque sodales aliquam. Morbi mollis neque eget arcu egestas non ultrices neque volutpat. Nam at nunc lectus, id vulputate purus. In et turpis ac mauris viverra iaculis. Cras sed elit a purus ultrices iaculis eget sit amet dolor. Praesent ac libero dolor, id viverra libero. Mauris aliquam nibh vitae eros sodales fermentum. Fusce cursus est varius ante vehicula eget ultrices felis eleifend. Nunc pharetra rutrum nibh et lobortis. Morbi vitae venenatis velit.
50
51 </p>
52
53 <p>
54
55 Quisque pellentesque sodales aliquam. Morbi mollis neque eget arcu egestas non ultrices neque volutpat. Nam at nunc lectus, id vulputate purus. In et turpis ac mauris viverra iaculis. Cras sed elit a purus ultrices iaculis eget sit amet dolor. Praesent ac libero dolor, id viverra libero. Mauris aliquam nibh vitae eros sodales fermentum. Fusce cursus est varius ante vehicula eget ultrices felis eleifend. Nunc pharetra rutrum nibh et lobortis. Morbi vitae venenatis velit.
56
57 </p>
58
59 <h3>Notice The Include Files</h3>
60
61 <p>
62
63 Quisque pellentesque sodales aliquam. Morbi mollis neque eget arcu egestas non ultrices neque volutpat. Nam at nunc lectus, id vulputate purus. In et turpis ac mauris viverra iaculis. Cras sed elit a purus ultrices iaculis eget sit amet dolor. Praesent ac libero dolor, id viverra libero. Mauris aliquam nibh vitae eros sodales fermentum. Fusce cursus est varius ante vehicula eget ultrices felis eleifend. Nunc pharetra rutrum nibh et lobortis. Morbi vitae venenatis velit.
64
65 </p>
66
67 <h3>Another Heading Starting Point</h3>
68
69 <p>
70
71 Quisque pellentesque sodales aliquam. Morbi mollis neque eget arcu egestas non ultrices neque volutpat. Nam at nunc lectus, id vulputate purus. In et turpis ac mauris viverra iaculis. Cras sed elit a purus ultrices iaculis eget sit amet dolor. Praesent ac libero dolor, id viverra libero. Mauris aliquam nibh vitae eros sodales fermentum. Fusce cursus est varius ante vehicula eget ultrices felis eleifend. Nunc pharetra rutrum nibh et lobortis. Morbi vitae venenatis velit.
72
73 </p>
74
75 <p>
76
77 Quisque pellentesque sodales aliquam. Morbi mollis neque eget arcu egestas non ultrices neque volutpat. Nam at nunc lectus, id vulputate purus. In et turpis ac mauris viverra iaculis. Cras sed elit a purus ultrices iaculis eget sit amet dolor. Praesent ac libero dolor, id viverra libero. Mauris aliquam nibh vitae eros sodales fermentum. Fusce cursus est varius ante vehicula eget ultrices felis eleifend. Nunc pharetra rutrum nibh et lobortis. Morbi vitae venenatis velit.
78
79 </p>
80
81 </div> <!-- end #content -->
82
83 <?php include('includes/sidebar.php'); ?>
84
85 <?php include('includes/footer.php'); ?>
86
87 </div> <!-- End #wrapper -->
88
89 </body>
90
91 </html>

Now make sure that we go inside of our nav.php file as well as the sidebar.php file and link the new page we have just created. Again using your html editor of choice, first open up your nav.php file.

Updating the links should make your file now look like this example below:

nav.php

1 <div id="nav">
2
3 <a href="index.php">Home</a>
4 <a href="about.php">About</a>
5 <a href="#">Portfolio</a>
6 <a href="#">Contact</a>
7
8 </div> <!-- end #nav -->

Next we will need to do the same thing to our sidebar.php file to make sure the links now work.

sidebar.php

01 <div id="sidebar">
02
03 <h3>Navigation</h3>
04 <li><a href="index.php">Home</a></li>
05 <li><a href="about.php">About Us</a></li>
06 <li><a href="#">Links</a></li>
07 <li><a href="#">Portfolio</a></li>
08 <li><a href="#">Contact</a></li>
09
10 <h3>Box Two</h3>
11 <li><a href="#">Link Here</a></li>
12 <li><a href="#">Link Here</a></li>
13 <li><a href="#">Link Here</a></li>
14 <li><a href="#">Link Here</a></li>
15 <li><a href="#">Link Here</a></li>
16
17 <h3>Box Three</h3>
18 <li><a href="#">Link Here</a></li>
19 <li><a href="#">Link Here</a></li>
20 <li><a href="#">Link Here</a></li>
21 <li><a href="#">Link Here</a></li>
22 <li><a href="#">Link Here</a></li>
23
24 </div> <!-- end #sidebar -->

Now we are complete. We have created a two page PHP website template that we can expand on more to make a completed project. We have learned how to use basic php includes and variables to be able to edit content accross many different pages at one time. I really hope you have enjoyed this post and learned something from it as well.

Just to take a final look at the new file structure with the added page you can view the image below for reference:

This tutorial was created to give you some basic knowledge on php and creating a php website template.

If you have any questions I will do my best to answer them in the comments below.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


  • None
  • Mr WordPress: Hi, this is a comment.To delete a comment, just log in, and view the posts' comments, there you will have the option to edit or delete them.

Categories

%d bloggers like this: