1. CSS
  2. Flash
  3. HTML
  4. Illustrator
  5. Java
  6. JavaScript
  7. Maya
  8. Photography
  9. Photoshop
  10. PHP
  11. Ruby
  12. Ruby on Rails
  13. 3ds Max

PHP: Database Driven Template Engine

  1. Clicks today:
  2. Clicks this month:
  3. Overall rating:

PHP » Content Management Systems — about 10 years ago
How to create a template engine with PHP & MySQL that stores all html and design within a database.


avatarVash about 10 years ago

Not to totally destroy the point of this tutorial but, what happens if your database software is on a different server from everything else, as is quite common these days? In short your site won't display if the database server goes down. Hence why it is best to store the templates in files. So at least your site and static resources will display even if your dynamic resources (data sent/received to/from the database) are not available. Probably quicker to use files too.

avatarnooty about 10 years ago

very good points indeed, but if you find your server going down then you need to be invseting in a better one! lol.

I prefer to use databases because it makes things for me easier to manage and edit. yes it has its flaws and drawbacks .. just like everything, but think abuot this - what if your server went down where your were storing all of yhour files?It's swings and rounderbouts really.

avatarEdR about 10 years ago

This tutorial is a BAD idea and demonstrates shockingly bad design, ideas, and code practices. Not that bad PHP is a surprise, but the point stands. You show little skill with the language and obviously do not understand how the LAMP stack works.

You should never be using raw MySQL queries unless you know what you are doing; you obviously do not. You should always be using a wrapper to allow for formatted queries in the form of prepared statements, specifically with PDO ( http://php.net/pdo ) or MDB2 ( http://pear.php.net/MDB2 ).

If you are going to use raw MySQL queries, you need to be at least somewhat cognizant of failure. This code is ripe for SQL injection when used by a less-than-competent programmer; a function to neuter any data passed as a query string is essential. (You do not need such a function when using PDO or MDB2; when you pass the prepared statement a variable it automatically handles variable neutering.) Given a naive user of this code, I would expect them to simply pass the value of a $_GET entry into engine::fetchtemplate(), at which point I simply pass the literal shown below in as that GET value:

unimportant'; EMPTY templates; SELECT * FROM templates WHERE templatename = '

The above string would cause your query to empty your database. Something tells me that's not quite a good idea.

Furthermore, the entire idea behind your incredibly basic "system" is foolish. Unless you know why you are storing your static pages as database entries, you're doing it wrong. It isn't "easier to manage and edit"; that's preposterous when you have the option of tools with built-in SFTP controls. This is wankery for the sake of wankery and does not provide any substantial benefits.

(Oh, and a tip: this doesn't "separate code and design" at all.)

Learn to program and learn to think before writing "tutorials" that will be more likely to harm learning programmers than help them.

avatarannyphp over 7 years ago

A theme engine that allows you to use template files written in pure php. These template files do not need to be processed by the theme engine, and as such execute a lot faster than most other template engines. Another major advantage to using PHP as your template language, is flexibility, as the advanced user can access any information / functionality available in the Drupal API, and is not restricted to what the template engine / language allows him to do. The template engine's job is to help separate out application model logic from display logic and to provide a simpler, more readable way to add functionality to your display files. Since HTML was never meant to handle arbitrary logic, it is generally kept separate from the Model and Controller layers of an application More PHP template engine project you can view:http://www.phpkode.com/projects/category/php-template-engine/

Your Comment

You must be logged in to post a comment.