PHP
  Home arrow PHP arrow Page 2 - Database Templating Engine
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
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? 
Google.com  
PHP

Database Templating Engine
By: Matt Eunson
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 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:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log 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

    - Implementing Factory Methods in PHP 5
    - Merging a File Split for FTP Upload using PHP
    - Getting Data from Yahoo Site Explorer Inboun...
    - Method Chaining: Adding More Selecting Metho...
    - How to Split a File During an FTP Upload Usi...
    - Expanding a Custom CodeIgniter Library with ...
    - Using the Yahoo Site Explorer Inbound Links ...
    - Building a CodeIgniter Custom Library with M...
    - Building an E-mini Trading System Using PHP ...
    - Completing the MySQL Class with Method Chain...
    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek