Home arrow PHP arrow Page 4 - Database Templating Engine

A Sample Page - 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.

  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



Now we have all the functions created, we can simply use the code that I wrote at the beginning of this tutorial.


// 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 = "My Site";

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

// And close the page


If you run this script, then the result will be the words "Welcome to my site" in your browser, along with the title "My Site" in your browser title bar.

And that just about rounds it up. Here you have a very simple, but working template engine using a database. You could extend this to include an admin control panel for editing your templates on the fly, or you could add new features to it. Anyway, thanks for reading this tutorial, and I hope you enjoyed it.

>>> More PHP Articles          >>> More By Matt Eunson

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


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