PHP
  Home arrow PHP arrow 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
    ( Page 1 of 4 )

    Have you ever been annoyed by template engines that force you to keep your templates in flat files? Ever feel like that Content Management System you just created isn't as dynamic as you originally thought? If you have ever felt like this, you may benefit from having your templates in a database.

    Some argue that getting your templates from a database might slow down your site, as well as adding the overhead of having to open a database connection. However, if you're using a PHP/MySQL driven site, you probably already have a open database link, so a few extra queries won't slow down your small site much.

    This tutorial will consist of two files, template.php and index.php. The first will have just have a few common functions (we could include these in index.php, but I prefer to separate function definitions from normal code). And the latter file, index.php, will connect to the database, and display the templates, amongst other things.

    When the code is finished, I should be able to have a page like this on my site:

    <?php

    // Simple Templating Engine
    // using a database tutorial.
    // Written by Matt Eunson

    // Define database variables - you have to change these
    $Server = "localhost";
    $Username = "me";
    $Pass = "****";
    $Database = "template";

    // Now connect to the database
    mysql_connect($Server, $Username, $Pass) or die("Couldn't connect to database.");
    mysql_selectdb($Database) or die("Couldn't select database: $Database");

    // Include the template functions
    include_once "template.php";

    // Start our page
    $Title = "Simple Template Engine using a Database";
    pageHeader($Title);

    // Some content here...
    echo "Welcome to my site.";

    // And close the page
    pageFooter();

    ?>

    Ok, now we know what we want, let's get this show on the road!



     
     
    >>> 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 6 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek