PHP
  Home arrow PHP arrow Page 3 - 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 - Functions


    (Page 3 of 4 )

    fetchTemplate function

    <?php

    function fetchTemplate ($templateName) {

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

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

    }

    ?>

    First, this assembles the query, and selects one row's content from the templates table, but only when it's the one asked for by $templateName. Then we perform the query, and store the resource into the variable $tRes. Next, we use the mysql_fetch_array(); function to store the result of the query into an array. The next part maybe confusing, so firstly, I'll explain what we do with the result of the function.

    To show the template page_header, we would use this code:

    <?php

    eval (fetchTemplate("page_header"));

    ?>

    This uses a function you may not have come across before, eval();. You can find more information about this function at www.php.net/eval. Basically, it parses the string you pass it as PHP.

    So if we go back up to the function fetchTemplate, and to the line $Content = "echo "$Template[content]";"; we can see that this is preparing the string for use with eval(); What it is doing is putting echo " in front of the template's content, and "; after it. If we use the short template page_footer as an example, the value of $Content would be the following:

    echo "</body>rn</html>";

    Therefore, when we use this string in eval, it would display the template's content onto the user's browser.

    Other Functions

    Now that we've got the hard bit out of the way, the pageHeader & pageFooter functions simply provide a nice easy function to put at the top & bottom of every page, instead of having to use eval (fetchTemplate("page_header"));

    However, if you remember the content of the page_header template, it contained a variable in it.

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

    And if we look at the pageHeader function, the $pageTitle variable is passed into it, so when the code is eval'ed in the function, $pageTitle is replaced with whatever was passed to it in the function.

    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 6 hosted by Hostway
    Stay green...Green IT