Database Templating Engine - A Sample Page
(Page 4 of 4 )
Now we have all the functions created, we can simply use the code that I wrote at the beginning of this tutorial.
<?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 = "My Site";
pageHeader($Title);
// Some content here...
echo "Welcome to my site.";
// And close the page
pageFooter();
?>
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.
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |