Intranet Journal   Earthweb  
Events Jobs Premium Services Media Kit Network Map E-mail Offers Vendor Solutions Webcasts

   Intranet Journal Subjects
Search Earthweb

Privacy Policy



internet.com
IT
Developer
Internet News
Small Business
Personal Technology

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers

internet commerce
Be a Commerce Partner
















 

[ Home | Discussion Forum | How Do I... | Lotus Notes Intranets | Microsoft SharePoint | Products | Shopping  ]

free news!


Creating a PHP-Based Content Management System, Part 6


Peter Zeidman
12/8/2004

Go to page: 1 2 3 4

Printer Friendly Version

Have a question about this article, object-oriented programming, or PHP? Visit Intranet Journal's Discussion Forum

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:

  •  Allow users with limited technical knowledge to create or edit content
  •  Allow the design of multiple pages to be changed by altering the template
  •  Maintain a uniform look & feel across the site
  •  Separate content from programming

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');
compilePage('template.tpl',$values);

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) {

$template = ereg_replace('{'.$key.'}',$val,$template);

}

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

Printer Friendly Version

Of Interest
Intranet Discussion Forum
Creating a PHP-Based Content Management System, Part 1
Creating a PHP-Based Content Management System, Part 2
Creating a PHP-Based Content Management System, Part 3
Creating a PHP-Based Content Management System, Part 4
Creating a PHP-Based Content Management System, Part 5
PHPBuilder

email this page

Tutorials
and more at:
Intranet Journal's Tutorials
Intranet Journal Favorites

Creating a PHP-Based Content Management System

The Spyware Guide

Introduction to Microsoft SharePoint Portal

Intranet Journal
Part of the EarthWeb Network

Managing Editor
Intranet Journal

Tom Dunlap

EarthWeb Home Page
Jupitermedia Home Page

Media Kit




The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers