iNET Interactive - Online Advertising Agency
          
   Home    Authors    About    Login    Contact Us
   Search:   
Advanced Search     
  Articles

  ASP (26)
  ASP.NET (19)
  C and C++ (4)
  CFML (2)
  CGI and Perl (16)
  Flash (2)
  Java (7)
  JavaScript (28)
  PHP (92)
  MySQL (13)
  MSSQL (3)
  HTML (35)
  SEO (9)
  Visual Basic (12)
  CSS (13)
  SSI (5)
  XML (12)
  C# (14)

  Developer News

July 3, 2008
Poll: Which Web editor do you use?
About
 
July 3, 2008
Book Review: Head First JavaScript
WebReference.com
 
July 3, 2008
10 Things You Can Do With a Wiki
About
 
July 2, 2008
Web Host Reviews - 10 New Reviews
About
 
July 2, 2008
14 Reasons You Should Join a Social Network
About
 
July 1, 2008
Mark Boulton's Freelance Design Secrets
SitePoint
 
Courtesy of moreover.com
 
Want to receive new articles via e-mail? Click here!
/Home /PHP /Templating

Making A Basic Templating System 

  Views:    7394
  Votes:    5
by Mark Klink 11/10/03 Rating: 

Synopsis:

What? You say you want to make a quick and easy templating system that has the ability to run your whole site? Well, then this article is for you.
Pages: firstback1 forwardlast
The Article

Buliding Your Template File

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

So let's open your favorite text editor.  I prefer CrimsonEditor personally.

In any case, open your favorite text editor, and create a new blank document.  Place the following code in your new document:

<html><body>
<table cellpadding="0" spacepadding="0" border="0">
<tr><td valign=top align=left>
<% maincontent %>
</td></tr>
</table>
</body>
</html>

The above is just a basic template. Your template can look however you want it to, as long as it contains the tag <% maincontent %>

where you want the content to appear in your pages.

Save the above page as template.html

Creating your 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:

 <?
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 %>", "$maincontent", $template);
                $template = eregi_replace("<% content %>", "$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 Your 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:

<?

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 it......enjoy!

Pages: firstback1 forwardlast

Similar/related articles:


 
  Sponsors