Leverage Dnyamic Content with Templates

General Development Concepts

Discussion of the best methods for serving dynamic content using templates.


Date : 2006-06-01
One of the best, most concise, methods for building a powerful database driven website that is easy to update, manage, and add on to is through using templates. I will endeavor to explain the concepts involved in working with templates as well as pointing out the fastest methods for best results. I will give some sample code in ASP but the concepts are valid in any language.

A template is an HTML page, or part of a page that can be merged with dynamic data to create a complete web page. These can range from entire pages, to very small snippets of HTML that can be used to build larger content areas. There are various methods for storing these templates. It is fairly common to store your templates in your database but I would not recommend this unless you have a good CMS setup to edit these templates. Most online editors that look like they would do a good job actually use really bad, outdated, HTML grammar so it would be better to have an editor that would just let you edit the HTML as text. My favorite method is to leave the templates on the disk, it is after all a database that is designed specifically for storing files of all kinds and retrieving them as quickly as possible. You can use any number of code editors to edit your HTML, but again, please avoid WYSIWYG editors especially for templates as they can mess with your template language and make for some really ugly pages.

I call it a template “language” but at it’s simplest it is basically the same as a mail-merge document. You have an outline with markers for where your dynamic content will be filled in that you process and output. When you create your templates you will need to decide on a format for your template markers. For instance many times I use the percent sign to designate a template marker. Here is an example of a simple template:

Code:
<html>
  <head>
    <title>%PAGE_TITLE%</title>
  </head>
  <body>
    %PAGE_CONTENT%
  </body>
</html>


In this simple template we have 2 markers one for the page title and the other for the page content.

Now we need our template processing function. There are many ways to approach this but the basic concept is to replace the template markers with the content needed for that page. With some template processors we have each specific content have it’s own template marker. For instance the main content of the home page would be %HOME_PAGE_CONTENT% while the main content of the contact page would be %CONTACT_PAGE_CONTENT%. This method is more often used if you are planning on building one template processor that any template can be passed through and output the correct content. This method is used most often in commercial CMS products. These products also build up a language in the templates where you can design loops and pull in database content just by adding the correct markers to your template.

I generally go with a more simplified template language and have specific code on each page to create the content for that page and fill it in to a more standard template. I generally can get away with two or three full page templates and a dozen or so smaller snippet templates for a full site. The content for these templates is stored in your database for retrieval and merging with the template.

The most important function in this process is doing the template marker replacement. There are a few different methods but the important feature is to make it as fast as possible. Your page could have 20 or 30 template markers on it and that many slow replace operations can really take up a lot of time. In general RegExp replace operations are fastest, and it can often be worthwhile to check for a template marker before creating the content for it. If there is no %AD_BOX% on this page, then why generate the Ad Box content.

Here is a very simple example of a template processor:

Code:
Function setTempMarker(sTemplate,sMarker,sContent)
  dim RE
  set RE = New RegExp
  with RE
    .Pattern = “%” & sMarker & “%”
    .IgnoreCase = True
    .Global = True
  end with
  setTempMarker = "" & RE.replace("" & sTemplate, "" & sContent)
  set RE = nothing
End Function


This function keeps from breaking with a null string, but it doesn’t do any trimming, content validation, or filtering. I add the template marker special characters in during this function (.Pattern = “%” & sMarker & “%”) because it makes it much easier to send the command:

Code:
  content = setTemplateMarker(template, ”TITLE”, “Home Page”)


Instead of constantly using “%TITLE%” every time you need to reference that marker. On the other hand if you don’t add the template marker characters in during the processor function you can use different characters to identify different kinds of content. For instance filling dynamic content into a narrow side box can end up breaking the layout because of overly long words or URLs entered as text. You could handle this kind of content with a different template character and your processing function could know to filter out long strings to avoid breaking the layout. Just remember that the more complexity you add into this operation the slower it will be.

So the basic steps to creating a page with a template are:
Load your template (from disk or database),
Check for a template marker,
Create the content,
Replace the marker with the content,
Output the template.

There are a lot of benefits to this kind of template based site hopefully this article will help you get started. Please feel free to contact me for any more help or examples in a different language. Just post a comment here and I’ll get back to you.

Comments :

No comments yet
  • Search For Articles