Home arrow PHP arrow Page 2 - Database Templating Engine

The Database - PHP

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.

TABLE OF CONTENTS:
  1. Database Templating Engine
  2. The Database
  3. Functions
  4. A Sample Page
By: Matt Eunson
Rating: starstarstarstarstar / 70
May 11, 2004

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: