Skip to main content

Awesome templating

Awesome Templating with XSLT

 

It's fair to say that XSLT has a bit of a "Love it or Hate it" relationship with the web development community. 

However the team behind ProteanCMS love XSLT and firmly believe when used correctly it is by far the best tool for the job.

Let us tell you why...

 

HTML is going knowhere

 

HTML drives the entire web, it is an open standard and is ratified by the W3C . So is XSLT  ! and it is the only templating language that is.

HTML is a type of XML  and XSLT is specifically designed for transforming one XML structure to another. Converting XML to HTML is the job we need to do.

XML is the most human readable way of presenting raw data from any object model, specifically we are refering to and object model containing the data around a page.

Look at the XML for this page

Alternatives are JSON which is briefer but much harder to read.

XML is the ideal format for the raw data for a webpage to be contained, converting it to HTML with XSLT is a beautiful, natural and elegant approach.

The syntax of XSL looks like HTML is very readable and clear for those already familiar with HTML.

 

Keeps the business logic away from the design

 

This is the key point, and where all other templating tools seem to fall down. For a web application 'such as a CMS' to be completely re-skinable, whilst remaining upgradable you need to ensure any business logic (what information gets shown on the page) completely separate from how that is presented.

CSS goes a long way to do this but not nearly far enough especially when using frameworks such as 'Twitter Bootstrap' flexibility to change every single element of HTML on a page is essential.

Tools like PHP and Microsoft's Razor allow business logic code that might call and present data to be written anywhere.

XSLT purely converts the data it allready has to the layout you want.

 

Overriding is a dream

 

In XSLT you can write a template to match any element. Consider a simple product

 
 
<product>
<name>widget</price>
<price>10</price>
<discountPercent>10</discountPercent>
</product>
 
 

You can then write a template to present, for instance a buy button in xslt

 
 
<xsl:template name="BuyButton" select="product">
<button>Buy<button>
</xsl:template>
 
 

This can reside in a series of common templates that reside at the platform level, so you always have a buy button.

However if you follow this in a bespoke template that follows the common templates you can do this.....

 
 
<xsl:template name="BuyButton" select="product">
<button class="FancyButton">Buy<button>
</xsl:template>
 
 

This will override the default template, so you can restyle all the buttons in your specific website.

If you want to get even more clever you can follow this with a more specific template follow the above with.

 
 
<xsl:template name="BuyButton" select="product[discount > 0]">
<button class="FancyButton discounted">Buy<button>
</xsl:template>
 
 

This is far more useful for web development purposes than IF THEN programming structures you would typically use in PHP or Razor. Once you get the hang of this web developers love it.

 

It forces perfectly compliant html

 

When working in a scripting lanaguge producing html it is very easy to miss a close tag in a table or a div, often this is hard to spot, and can produce unpredictable results, that may not come to light until later down the road.

 

It is seriously powerful

 

XSLT has a wide range of logic constructs and the ability to create a reuse all kinds of useful functions and features.

It can also be extended easily, for instance EonicWeb has a clever function that can be called from XSLT which resizes images on the fly.

In 12 years of development I have never found something I cannot do in XSLT (that should be done on the presentation layer)

 

It is fast....

 

nuff said...

 

It is a global standard and platform independant

 
 
 

Why it is out of vogue... and why that doesn't matter.

 

Microsoft have shifted the focus away from XSLT as a template language, in favor of there Razor framework. Razor is proprietary to Microsoft and is not a pure template language.

Razor allows any C# coding to be added to a template, it allows you to do all sorts of nasty business logic from the "View" and is very open to abuse. Making a product difficult to upgrade and provide continued support.

Razor is also very venerable to changes as Microsoft release new versions of the MVC framework. Therefore making the upgrade path for a website CMS platform unwieldy.

If you like lazy hacks, spaghetti code and being locked in to providing support for legacy systems and (many developers get their bread and butter this way) then Razor is for you.

XSLT has continued cross platform support from Saxonica the original authors of the XSLT standard and XSLT version 3 has recently been approved by the W3C.

XSLT remains the best tool for the job hands down.