Smarty vs. XML/XSLT

There are many ways to separate business-logic and data presentation of an Internet solution in PHP. But the most popular one is usage of templates. The idea behind the method is to use some kind of “engine” that binds visual data representation with the data of a page. There are many advantages in using templates:

• Ease of design change
• Ease of interface localization
• Possibility to work separately on design and code by different people at one and the same time
• Etc.

But the goal of this article is not to make templates even more popular. We want to briefly compare two of the most popular methods of separating data and its presentation – templates and XML/XSLT. We will use Smarty as a representative of the template method.

Smarty is a PHP class that can be used either as an extension or with the use of “include”:

<?
require_once(“Smarty.class.php”);
?>

As Smarty authors say, Smarty is a “template compiler”. And what exactly is this Smarty’s “compilation”? If we carefully study the files produces by Smarty (it needs a folder with permissions to write files into it), we will notice Smarty’s compilation is a simple replacement of the {$var} variables into PHP code like:

<?php echo $this->_tpl_vars[‘var’]; ?>.

Actually, the replacement operation is done only once, when Smarty finds changes in the original template file. This template engine uses the “compiled” template file to generate pages after the initial replacement. Smarty uses caching (which can be turned off) to increase performance of results generation.

A developer basically needs to study a new language (another one 🙂 ) to use Smarty in a project.
The Smarty “language” is carefully described in a PDF documentation of around 150 pages.

In around 80% of cases, developers use only a few standard operators and functions out of the huge amount of ones offered by the “engine”.

One should use the “assign” method to send the value of a variable to the template:

$oSmarty = new Smarty();
$oSmarty->assign(“article”, ”Article”);
$oSmarty ->display(“template.html”);

The “display” method generates a page with data based on the “template.html” template file.

And to show the variable, one should use the following code in the right place of the template:

Some code

{$article}

Some code

One should use “assign” to display more than one variable:

$oSmarty = new Smarty();
$oSmarty->assign(
“var1″=>”Article”,
“var2″=>”Article2”,
“var3″=>”Article3”,
);

And the template file should contain the following code:

Some code

{$var1}

Some code

{$var2}

Some code

{$var3}

Some code

One should use associative arrays for complex data structures:

$oSmarty = new Smarty();
$article = array(
“key1”=>”value1”,
“key2”=>”value2”,
“key3”=>”value3”
)
$oSmarty->assign(“article”, $article);

And the following code in the template file:

Some code

{$ article.key1}

Some code

{$ article.key2}

Some code

{$ article.key3}

Some code

Smarty supports conditional constructions “if…else” in the following way:

{if $var >0 }
Some code
{else}
Some code
{/if}

One should use the “section” method to display repetitive data blocks:

{section name=article loop=$articles}
<A HREF=”{$article_link}/{$articles[article].art_id}”>{$articles[article].title}</A><BR>
{/section}

We will stop dwelling on Smarty now. And we can recommend reading the documentation of Smarty (you can find one with the distributive) to those interested in the engine.

Basically, Smarty is a jumble of PHP code and HTML tags. It only slightly eases developers’ work. The “dirty” work to place the PHP code into template is done by the parser. In exchange for the dirty work done, the developer has to study a new, non-standard “programming” language. And though the language is powerful enough, it still has its own limitations, as any other language has.

Let us look at the major Smarty competitor – XML/XSLT.

XML/XSLT is a specification of a language, offered by the W3C. As a result, XML/XSLT is supported by a much huger amount of parsers, than by one (Smarty), even very good one.

That is why, XML/XSLT support in PHP is possible with different parsers. We will take Sablotron parser as one of the representatives of many parsers for XML/XSLT for our example.
But there are other ones. And as soon as there is another parser (e.g., a faster than Sablotron one) that me might like more, we can easily switch to the new parser without any need in studying a new language and modifying the code for new functions or operators. We will simply have to change a few instructions that call the parser. Due to the “standardization”, one can create platform-independent templates for porting an application to another development language (e.g., from PHP to Java, or ASP.Net).

There is no need to show the XML/XSLT specification in this article. Anyone can access it from the W3C web site. The only thing that we can mention is that the parser of our choice – Sablotron – does not support the specification fully nowadays. However, a new release of Sablotron or another parser can easily support the specification better.

It is not more difficult to use XML/XSLT than to use Smarty, as typically one uses only a few typical constructions to develop new applications.

One should install the Sablotron parser on the server and compile the PHP with the –enable-xslt –with-xslt-sablot options.

Here is a simple example of XML/XSLT work in PHP with the usage of “XSLTransformer” class:

<?php
include(“class.xslt.php”);

$t = new XSLTransformer(‘test.xml’, ‘test.xsl’);

print $t->getOutput();

$t->destructXSLTansform();
?>

The ”test.xml” file contains the following data:

<?xml version=”1.0″ encoding=”UTF-8″?>
<cl>
<toptitle>Jack’s CV</toptitle>
<text> Jack has 4 years experience teaching in university and 3 years in the industry. He has interest in distributed and parallel computing and parallel processing.
</text>
</cl>

The ”test.xsl” file contains the following code:

<html>
<head>
</head>
<body>
<table >
<tr>
<td><xsl:value-of select=”/cl/toptitle”/></td>
<td><xsl:value-of select=”/cl/text “/></td>
</tr>
</table>
</body>
</html>

One can use dynamically generated data instead of files:

<?php
// $xml and $xsl contain the XML and XSL data

$xml = “<?xml version=”1.0″ encoding=”UTF-8″?>
<cl>
<toptitle>Jack’s CV</toptitle>
<text> Jack has 4 years experience teaching in university and 3 years in the industry. He has interest in distributed and parallel computing and parallel processing.
</text>
</cl>”;

$xsl=”<html>
<head>
</head>
<body>
<table >
<tr>
<td><xsl:value-of select=”/cl/toptitle”/></td>
<td><xsl:value-of select=”/cl/text “/></td>
</tr>
</table>
</body>
</html>
”;

$arguments = array(
‘/_xml’ => $xml,
‘/_xsl’ => $xsl
);

// Allocate a new XSLT processor
$xh = xslt_create();

// Process the document
$result = xslt_process($xh, ‘arg:/_xml’, ‘arg:/_xsl’, NULL, $arguments);
if ($result)
{
print $result;
}
else
{
print “Sorry, xml could not be transformed xsl into”;
print ” the \$result variable the reason is that ” . xslt_error($xh) .
print ” and the error code is ” . xslt_errno($xh);
}
xslt_free($xh);
?>

The major draw-backs of XML/XSLT code and appearance separation via Sablotron are slow speed of transformation and a need to re-compile PHP to support the parser. However, usage of some other parser can eliminate these draw-backs.

As a conclusion, we want to tell that it is basically a matter of preference on whether to use Smarty (or any other template “engine”) or XML/XSLT, as both the two major methods have their pros and cons. In some cases, it is better to use Smarty, especially if you do not have full access to your server and cannot compile XSLT support into PHP. In many other cases, the power and flexibility of XML/XSLT beats Smarty (and other dozens of currently available template “engines”) easily in most of the aspects of its usage.

About the Author:Sergey Makogon