Article Diary - General Knowledge Digest

Making A Basic PHP Template System

One of the most commonly asked questions related to PHP template programming is "How to make a basic template system in PHP?" So this tutorial explains how to make a basic template system in PHP with example codes.

Building Basic PHP Template System With PHP

It requires a total of 3 pages to create a very basic PHP template system. The files you will be creating are as follows:
  1. index.php
  2. template.html
  3. content.php
The PHP script content.php will be where all of your content is stored, template.html is your HTML template and index.php is the PHP script that puts them all together, and runs your site.

Now, there are many types of template systems. However, they all have the same concept, and pretty much work the same. The difference is, some template systems use a large number of pages, for each content portion of your site. Since that system still leaves you to create a new page for each portion of your content, it’s probably not as efficient. So let’s get started!

Building Your Template File

The first file that should be created is your template file: template.html

So let’s open your favorite text editor. In any case, open your favorite text editor, and create a new blank document. Place the following code in your new document:
Source Code - template.html
The above is just a basic template. Your template can look however you want it to, as long as it contains the tag where you want the content to appear in your pages. Save the above page as template.html

Creating index.php Script

Now, of course, we need to create the functioning portion of your site. So with the same trusty text editor, create a new document and place the following code in it:
Source Code - index.php
function template($content)
{
global $maincontent;
$filename = “template.htm”;
if(!$fd = fopen($filename, “r”))
{
$error = 1;
}
else
{
$template = fread ($fd, filesize ($filename));
fclose ($fd);
$template = stripslashes($template);
$template = eregi_replace(“”, “$maincontent”, $template);
$template = eregi_replace(“”, “$content”, $template);
echo “$template”;
}
}

function cmd()
{
global $maincontent;
include (“content.php”);
template(“$data”);
}

switch($action)
{
default: // default switch
home();
break;
}
?>
Save the above document as index.php, and let’s move on.

Create content.php File

Now this file will contain all of your content, and will serve the content, along with your template file, into your index.php file, conditionally based on the variables passed in the URL. Create a new document, and add the following code. Hopefully you will see the pattern in this document, that will allow you to keep adding content to it, and break it up into content sections:
Source Code - content.php
if($go == "")
{
$maincontent .= ’This will be the default content upon entering the page,
if no variable is set’;
}
if($go == "prices")
{
$maincontent .= ’This would display your pricing content’;
}
if($go == "contact")
{
$maincontent .= ’This would display your contact form and information’;
}
?>
Save the above document as content.php, upload all three files to your server, and test it by going to index.php. You should first see your welcome message. Now, if you change the url to index.php?go=prices This will then display your pricing information and so on.

Note: While you can include html within your maincontent tag, in the document above, you must prefix all quotes “ with a backslash ” This will ensure that all html displays properly.

That’s all there is to create a very basic template system in PHP.

Written by: Mark Klink
Attached Files
There are no attachments for this article.
Comments
There are no comments for this article. Be the first to post a comment.
Name
Email
Security Code Security Code
Related Articles RSS Feed
How do I setup and use SSH?
Viewed 980 times since Thu, Oct 15, 2009
How to change the file type using htaccess?
Viewed 669 times since Sun, Mar 14, 2010
New Tags Introduced in HTML5
Viewed 926 times since Sat, Apr 3, 2010
Create Glowing, Radioactive Buttons with CSS
Viewed 657 times since Sun, Dec 27, 2009
Free Under Construction Coming Soon Template
Viewed 1366 times since Sat, Mar 12, 2011
Review of Mobile Web Application Frameworks
Viewed 953 times since Mon, May 9, 2011
Create Dynamic Tabs with jQuery
Viewed 647 times since Sat, Feb 6, 2010
ValidForm Builder - Creating Forms With Validation
Viewed 626 times since Wed, Apr 21, 2010
How can I change session timeout value?
Viewed 652 times since Tue, Apr 27, 2010
Free Android GUI PSD File
Viewed 927 times since Fri, Dec 4, 2009
MENU