Home arrow PHP arrow Page 3 - Database Templating Engine

Functions - 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

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
 

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: