|
|
|
|
|
|
|
|
Creating a PHP-Based Content Management System, Part 6
Peter Zeidman 12/8/2004 Go to page: 1 2 3 4
Templating Systems It would be useful to build some kind of templating system into our CMS. Note that the PHP needed for this is a bit more advanced than previous articles, but the complete code will be available for download. The templater should perform a number of tasks:
There are a number of approaches to this. The most basic is to simply use includes — put all the content into separate files, and insert the correct ones where needed by using the include($filename) PHP command. This isn't a great solution. The files will build up in an disorderly fashion, and you'll defy the point of having a Content Management System in the first place. Pattern Matching A simple but elegant solution is to build a pattern matching engine. For example, you'd create a template file including some code such as: <i> My name is {name}, and my favourite food is {food}</i> And you'd save that in template.tpl ('.tpl' to denote it's a template). You could then create pages with content, for example one called susan.php would contain the code: $values = array('name' => 'Susan','food' => 'chocolate'); And the compilePage function would combine the template with the content, to give: My name is Susan, and my favourite food is chocolate Which would be displayed to the user. By having one template file with multiple content pages, you fulfill the goals of keeping a uniform look and feel, separating out content from code, and allowing the site's design to be changed by altering one template. This is a perfectly adequate solution, although one of our goals is to "allow users with limited technical knowledge to create pages". To save users from having to edit PHP files directly, it would be good to store the page content in the database. This has the advantage of allowing for the content to be easily searched, and to be stored in an orderly fashion. The complete code for this system is beyond the scope of this article and will be available for download, however here are the most important lines: foreach ($replaceTags as $key=>$val) {
} This has the function of converting tags, such as {food}, and replacing them the words specified in $replaceTags. In the above example, {name} would be replaced by 'Susan', and {food} by 'chocolate'. The newly formed page is now stored in $template, and we can do with it what we please. A disadvantage of this solution is the need for the above to code to run every time the user views a page. On a large website or Intranet, this could get pretty slow. The answer is only to do this once, and save the combined template and page content into a plain .htm or .php file for the user to view. This is called "static page generation", and greatly reduces server load. Alternatives The templating system I'm providing in the downloadable code is only a simple one, and there are very popular and feature-laden systems freely available online. Perhaps the best known one is called Smarty (see http://smarty.php.net). It has various advanced features, although I'll be sticking with my original goal — to create a system fully from scratch, so that I can understand every nook and cranny of its insides. What additional features could be included in a full Content Management System? Read on...
Go to page: 1 2 3 4
|
Intranet Journal's Tutorials |
|
Managing Editor |