Tuesday, December 25, 2007

XSLT or Template Engines

Having a nice separation between business logic and presentation logic in web development is a really beneficial and important functionality to be obtained. There are many template engines for any server-side technology out there such as: Smarty, Heyes Template Class, FastTemplate, ShellPage, STP Simple Template Parser, OO Template Class, SimpleTemplate, bTemplate for PHP scripting language and Velocity, FreeMarker, SiteMesh, Better Templates for Everybody, WebMacro, Transformica, Tea, Jamon, StringTemplate, jxp for JSP. There are lots of them but there are many common features in all of them.
Usually 2 goals are achieved by using a template engine:
  1. Separation of the business logic and presentation logic
  2. Separation of the server-side scripts and HTML code. (logic and content)

The former is the right goal! and the latter shouldn't be a direct goal. The separation of HTML and Scripting language will achieve in some extent but we shouldn't go deliberately for gaining it! For example assume that we want to display a table-based report of all customers who have bought something in the last month in 3 columns. Executing the query and fetching the result should be happened in the business logic and separated from HTML code. But dividing the customers in 3 groups in the business logic is a wrong solution! Because in a new layout (template design) we may want the customers in 4 columns, so we should achieve this without amending the business layer. So our template engine should have the features to let us have some logic in the templates (presentation logic) in this example we need to have FOR loops or similar structures in order to display the group of customers in 3 columns. This is exactly what is done by Smarty and XSLT. But there are some problems with using the template engines which are:
  1. They usually have complex process and documents.
  2. Some of them are replacing the standard and popular scripting language e.g. PHP with a more complex and non-standard conventional language
  3. The business layer usually produce a non-standard and non-structured data which is totally useless to other websites or applications
  4. They usually (NOT always) bring some security risks if we have untrusted template developers.



Using XSLT realize the main goal which is separating the business logic from presentation logic. We can have powerful logical structures in the presentation layer which could not be malicious as well. Generally XSLT will bring the following benefits:
  1. The XSLT documents are not more than some XML files and HTML files.
  2. XSLT could be as complex as some of the template engines but there are lots of books and resources out there due to its standard and support from W3C.
  3. The data which is produced by business layer i.e. XML is 100% standard and reusable by other programming technologies.
  4. The XSLT logical structures could not be used in a malicious fashion.





Download the e-book: Beginning XSLT 2.0 from Novice to Professional

2 comments:

Amir H. Fassihi said...

I think good points about XML/XSLT are:

Being standard as you mentioned.
Enabling large inter-website integrations so that the content of one website can be published on another website, leading to SOA.

I wanted to say that the down side of XSLT is that it is not a Turing Complete language, but I just looked around a little and found out that it is so in that respect id does not have anything less than the usual template engines.Although usually strong computations should be avoided on the UI side.

So I'd say : XSLT.

Seven said...

yeah, as I've read the creators of XSLT have avoided to make it Turing Complete in order to obtain some kind of independency which I've forgotten.