|home | news | documentation | source | downloads | discussion | projects | license|
Comparison with PHP, ASP, JSP
Comparison with XML/XSLT
Clearsilver Compared: vs. PHP, ASP, JSPby David Jeske <firstname.lastname@example.org>
There is one overriding goal of any HTML template system. That goal is to support the separation of the HTML presentation from your application logic. When systems do this properly, they are great templating systems. When systems do this poorly, they may be good Dynamic HTML programming systems, but they are not great template systems. This paper reviews different types of template systems to explain why dataset driven templates, like Clearsilver, offer the best combination of features for HTML templating.
Comparison OverviewRobust HTML Template systems can loosely be categorized into the following three styles: code in template, callback in template, and dataset driven template. Here is a breakdown of some template systems and which categories their common programming models fall into:
Systems to the left are faster to write one dynamic page in, and generally have a very quick learning curve for those new to HTML programming. Systems on the right are faster and easier for adding new pages to an existing site. Systems on the right are also more robust and flexible when writing, maintaining, and changing the look of many pages over time.
Code in TemplatePHP is the best examples of a full code in template system. It is distinct from other systems because there is no separation between your code and your HTML templates. When you include your common library code, it is also sitting in Templates, all of which can output HTML. This offers the quickest route to writing a simple dynamic page. Programming can start with a static HTML mockup, and parts of the mockup can be incrementally replaced with programming logic, database lookups, and loops. If you simply need a system to quickly write a few dynamic pages, and you don't anticipate needing to change the look of the pages, this is the right kind of template system to use. This type of template system is ideal for applications such as small internal IT tools and small or free websites.
The drawback of using a code in template system, is that your code and HTML output quickly become forever intertwined. This makes changing the appearance of your pages difficult. For example, if you check the user cookie and load user database data in the "common-header" moving around where you include this template will change where you retrieve the database information for the user, possibly breaking other parts of the page which rely on that data.
It is possible with PHP or any other code in template system to choose to rigidly separate your application logic from your presentation code. To do this, all application logic is placed first in the template. This code merely fills in global variable strings which you use later to render the page and does not output any HTML. This method is successfully used in some PHP scripts, however, PHP does not especially help you do this, and as you'll see below, template systems designed to natively do this have additional features to make this type of programming easier.
Callback in TemplateASP (VB/C# embedded in HTML) and JSP (Java embedded in HTML) are both callback in template systems. This is because they encourage a two tier model where your application code is written separately and the template calls into your application code. This has the advantage that your application logic is largely separated into a separate place, and the page is mostly just triggering different actions and display actions to occur.
However, because these systems use a callback model, they have less than robust template rendering capabilities. Often ASP and JSP pages call into a callback to "render an html chunk". This happens most often because the looping functionality in these types of template system is not robust enough to easily iterate over a collection of items and access the sub-data of those items. This creates a hard to maintain mess of display logic which is split between your callbacks and your template, making it difficult to change the appearance of your pages without breaking their function. It also makes adjusting the presentation of pages accessible only to 'real programmers' who are very familiar with the code.
It is worth noting that PHP can be used as a callback in template system as well, by writing the bulk of the application logic in C (or separate PHP templates) and exporting those library functions to PHP. However, PHP is more often a combination of code-in-template and callback-in-template styles, not one or the other.
Dataset Driven TemplateDataset driven templates are a newer style of web programming which is gaining popularity. This type of template system is designed to enforce the separation of HTML presentation and application logic. Like callback in template systems, your application logic is separated into a separate library of code in your language of choice. Like code in template systems, robust looping and macro functionality are provided for building your appearance. In many ways these systems offer the best of both worlds, quick and easy webpage developement, and flexibility in changing your site appearance. Unlike either other style, there is no application logic in the template, only presentation code.
This separation is created by dividing page rendering into two distinct steps. In the first step your application logic runs and outputs its data into a static dataset. If you are familiar with command-line shells in UNIX and Windows, it will be easy to understand this by way of analogy. This static dataset is very much like the environment variables of your shell. They are a set of name/value relationships which are merely static strings. If your web application has the concept of a logged in user, then the dataset might contain the data "user=david".
In the second step your presentation template code runs. It only has access to this static dataset to do its job, and all work of read and writing to files or your database has already been completed. Using an approach where templates cannot do any real work assures that the order that HTML is output into the page is irrelevant. If you want the user information at the top, you simply reference that data at the top of your template, if you want it at the bottom, you reference it at the bottom.
This model offers many advantages over other template models. Here are a few of them:
ConclusionWith these and many other advantages, dataset driven templates might just be the ideal way for you to simplify programming your web-based application by cleanly separating your application logic from your presentation templates.