PHP
  Home arrow PHP arrow Page 2 - Database Templating Engine
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Mobile Linux 
App Generation ROI 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

Database Templating Engine
By: Matt Eunson
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 2 stars2 stars2 stars2 stars2 stars / 70
    2004-05-11

    Table of Contents:
  • Database Templating Engine
  • The Database
  • Functions
  • A Sample Page

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Database Templating Engine - The Database


    (Page 2 of 4 )

    First, let's decide on our database structure. For what we need to do, we only require one table, which will be called "templates," which will contain -- yes, you guessed it -- our templates. It's a very simple table and only has 3 fields.

    • templateid: This is a unique identifier for the template.
    • name: Pretty self explanatory, the name of the template.
    • content: Again, pretty obvious, this is the HTML that makes up the template.

    To create this table, you can use the following SQL:

    CREATE TABLE `templates` (
    `templateid` int(10) NOT NULL auto_increment,
    `name` varchar(30) NOT NULL default '',
    `content` text NOT NULL,
    PRIMARY KEY (`templateid`)
    ) TYPE=MyISAM AUTO_INCREMENT=1 ;

    Now we will populate it. We will only have 2 records, page_header & page_footer. These will be used to display the <html>, <head> and other needed tags in the page. This is simple so that you can understand what is happening, in reality, these would include logos, menus, and other elements. The advantage of this is that there is only one place where you edit the layout of your page, instead of having to do it in each HTML file. Anyway, here's the SQL:

    INSERT INTO `templates` VALUES (1, 'page_header', '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">rn<html>rn<head>rn
    <title>$pageTitle</title>rn</head>rn<body>');
    INSERT INTO `templates` VALUES (2, 'page_footer', '</body>rn</html>');

    Let's go through page_header, which consists of the following html:

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
    <head>
    <title>$pageTitle</title>
    </head>
    <body>

    As you can see, this includes a PHP variable, $pageTitle. These will be defined in the code before we display the template. But we'll talk about that later. Now we have the database schema ready, we can start the basic functions for grabbing the template from the database.

    template.php

    The following code is in the file 'template.php':

    <?php

    function fetchTemplate ($templateName) {

        $Query = "select content from templates where name='$templateName'";
        $tRes = mysql_query($Query);
        $Template = mysql_fetch_array($tRes);

        $Content = "echo "$Template[content]";";
        return $Content;

    }

    function pageHeader($pageTitle) {

        eval (fetchTemplate("page_header"));

    }

    function pageFooter() {

        eval (fetchTemplate("page_footer"));

    }

    ?>

    This may seem a bit confusing, so I'll go over some parts more closely.

    More PHP Articles
    More By Matt Eunson


     

       

    PHP ARTICLES

    - Working With Different Namespaces in PHP 5
    - User Management Explained: Overview
    - Using Namespaces in PHP 5
    - Database Security: Guarding Against SQL Inje...
    - Building a Modular Exception Class in PHP 5
    - Database and Password Security for Web Appli...
    - Handling MySQL Data Set Failures in PHP 5
    - Building Site Registration for Web Applicati...
    - Intercepting Customized Exceptions in PHP 5
    - Securing Your Web Application Against Attacks
    - Sub Classing Exceptions in PHP 5
    - Authentication for Web Application Security
    - Building a Content Management System with Co...
    - Filters and Login Systems for Web Applicatio...
    - Working with the Email Class in Code Igniter





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway
    Stay green...Green IT