Home arrow PHP arrow Database Templating Engine

Database Templating Engine

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

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
 

blog comments powered by Disqus
   

PHP ARTICLES

- 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...
- PHP: Building Concrete Validators
- Sanitizing Input with PHP
- Executing Shell Commands with PHP
- Handling File Data with PHP
- File Security and Resources with PHP
- ArrayObject PHP Class Examples
- ArrayObject PHP Class: An Introduction
- Getting File System Data with PHP
- PHP Tools for Working with the File and Oper...
- Working with the File and Operating System w...
- PHP Proxy Patterns: Completing a Blog


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 5 - Follow our Sitemap

Dev Shed Tutorial Topics: