MVC | Smarty Templates vs. XSL Transformation

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

WebbieDave
Forum Contributor
Posts: 213
Joined: Sun Jul 15, 2007 7:07 am

Post by WebbieDave »

AKA Panama Jack wrote:It can... :)
I should know since I developed a faster and smaller version of Smarty.
That's not really Smarty then now is it. It's some sort of delicious Low-Fat-Smarty you've cooked up. Kudos!
AKA Panama Jack wrote: You have to remember that the template file is onlay parsed ONCE and converted into a PHP program that is called everytime afterwards without any need to parse the original.
But I also have to remember that my pages still have to load the bloated Smarty class everytime, plus perform multiple stats to check if it needs to "recompile" (or "deprogram" as I like to say) my templates from smarty back to PHP. Using PHP as your template engine eliminates all that.

I renamed the class to Webview and added some more variables to mimic Smarty's (just for timing).

Here's the class:

Code: Select all

<? // Try it yourself!
class Webview
{	
	public $template_dir = '';
	public $compile_dir = '';
	public $config_dir = '';
	public $cache_dir = '';
	
	private $_templateVars = array();
	
	public function assign($name, $value) 
	{
		if (!strlen($name)) return;
		$this->_templateVars[$name] = $value;
	}
	
	public function display($_viewName) 
	{
		foreach ($this->_templateVars as $_key => &$_val) {
			$$_key = $_val;
		}
		$_errorSetting = ini_get('display_errors');
		ini_set('display_errors', 0);
		include $this->template_dir . $_viewName . '.tpl.php';
		ini_set('display_errors', $_errorSetting);
	}	
}
?>
Here's the template:

Code: Select all

<table>
	<tr><th>Key</th><th>Value</th></tr>
<?foreach ($myArray as $key => $val):?>
	<tr><td><?=$key?></td><td><?=$val?></td></tr>
<?endforeach;?>
</table>
Here are the results (averages):

Smarty: 0.0220789909363 sec (does not include the original parsing!)

Webview class: 0.00203585624695 sec

That's 10 times slower folks. What miracle is Smarty providing us that would justify my pages running 10 times slower (not always that much slower, but a lot of the times - and sometimes even slower)? I can't think of one. So, as a general rule, I don't use Smarty unless my boss forces me to. I just use a simple class similar to the one above.

Also, as an aside (and since many of us here seem to actually be interested in implementing an MVC-ish framework), you'll notice the Webview class stresses the use of named views, like

Code: Select all

$t->display('product_listing')
rather than

Code: Select all

$t->display('product_listing.tpl.html')
as this enchances the separation we're trying to achieve in the first place.
User avatar
kyberfabrikken
Forum Commoner
Posts: 84
Joined: Tue Jul 20, 2004 10:27 am

Post by kyberfabrikken »

WebbieDave wrote:

Code: Select all

foreach ($this->_templateVars as $_key => &$_val) {
  $$_key = $_val;
}
http://www.php.net/manual/en/function.extract.php
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

Code: Select all

<?foreach ($myArray as $key => $val):?>
That's not very portable code you have there mate.

This aside, however, using PHP as a templating language makes a lot of sense; that's what it was created for. Unfortunately, unless you have a well disciplined development team, allowing arbitrary PHP code will cause business logic to start slipping into your templates. What's nice about XSLT (and Smarty, to a certain extent) is that it makes doing that extremely difficult.
Oh, and how can Smarty (or XSL transformations, for that matter) ever be as fast or flexible as doing something like this?
XSLT transformations are pretty dang fast. Not as fast as native PHP, but they are completely implemented in C and are fairly quick. As for flexibility, well, I consider it a feature that XSLT that you cannot query the database from an XSL transformation file.
User avatar
AKA Panama Jack
Forum Regular
Posts: 878
Joined: Mon Nov 14, 2005 4:21 pm

Post by AKA Panama Jack »

It's kind of hard for anyone to verify what you posted because you didn't post the entire code for what you used to make both tests. We have no idea how many iterations were made either.

Don't just post code snippets to try and make a point. You have to make available the exact code you used to make the tests so they can be independently verified by anyone. Otherwise... well... you know. :)
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

I once bought into the XML/XSL story, but experience learned me that it requires lots and lots of memory and didn't scale very well... With templates in PHP and Smarty i got much better results and was able to handle 1000hits/minute...

A couple of years ago i found it much harder to find webdesigners that were able to generate standards compliant xhtml, which made the XSL story harder too... These days i would go for the solution that requires the least 'expertise', which would be plain-old-php... (And offer a nice interface of the business logic to the designer and code monkeys, meaning they would have to implement againt a mock.. Afterwards i'd replace the mock with the actual code)
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

I once bought into the XML/XSL story, but experience learned me that it requires lots and lots of memory and didn't scale very well... With templates in PHP and Smarty i got much better results and was able to handle 1000hits/minute...
Having worked with PHP's manual (which is DocBook XML compiled using XSLT stylesheets) I can certainly vouch for this assertion.

I suppose XSLT would work well for fairly static documents which can be cached. Also, the transformation code itself could be inefficient.
A couple of years ago i found it much harder to find webdesigners that were able to generate standards compliant xhtml
That's not necessary at all. All they need to do is created valid XML, and everything else follows. XSLT won't stop you from putting a div inside bold tags, but it will stop you from forgetting to close tags, etc.
These days i would go for the solution that requires the least 'expertise', which would be plain-old-php... (And offer a nice interface of the business logic to the designer and code monkeys, meaning they would have to implement againt a mock.. Afterwards i'd replace the mock with the actual code)
XSLT is nice because there's no need for a mock: you just supply a sample XML file to transform from.
WebbieDave
Forum Contributor
Posts: 213
Joined: Sun Jul 15, 2007 7:07 am

Post by WebbieDave »

Sweet. Thanks. Humbled.
Ambush Commander wrote:

Code: Select all

<?foreach ($myArray as $key => $val):?>
That's not very portable code you have there mate.
Oh, sorry. Here ya go:

Code: Select all

<?php foreach ($myArray as $key => $val):?>

j/k. I just through that in the example template to look more Smarty-ish :)
Ambush Commander wrote:unless you have a well disciplined development team, allowing arbitrary PHP code will cause business logic to start slipping into your templates.
So sad but so very, very true. Does this mean if Smarty is being used, there must be an undisciplined development team nearby?
AKA Panama Jack wrote:It's kind of hard for anyone to verify what you posted because you didn't post the entire code for what you used to make both tests. We have no idea how many iterations were made either.

Don't just post code snippets to try and make a point. You have to make available the exact code you used to make the tests so they can be independently verified by anyone. Otherwise... well... you know. :)
If you're not gonna believe my numbers, you're not gonna believe my number of iterations either :) j/k The only code missing was the code that used the Smarty/Webview class. I just figured the aficionados here, yourself included, would understand that just the most basic code was being used: new Smarty/Webview, fill config vars, assign array(1,2,3), display. Either way, we both agree Smarty is cool. We just disagree on the tradeoffs. I say more code equals more execution cycles equals slower. I work on a high traffic site, so I'm always forced to find faster ways (which, of course, doesn't always mean better).
Ambush Commander wrote: XSLT is nice because there's no need for a mock: you just supply a sample XML file to transform from.
I'm beginning to think that it's all headed this way anyway.

Good talk, everybody.
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

Does this mean if Smarty is being used, there must be an undisciplined development team nearby?
Enforcement of separation between presentation and business logic is one of the professed benefits of Smarty. It also has a compact syntax.
WebbieDave
Forum Contributor
Posts: 213
Joined: Sun Jul 15, 2007 7:07 am

Post by WebbieDave »

I was just joking. But, yes. It is a benefit. Even though one could do anything in Smarty tags that you could do in PHP with {php}{/php}. Am I correct in that? Nevertheless, the chances of confusing biz logic with display logic are greatly lessened when using something like Smarty.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

Ambush Commander wrote:XSLT won't stop you from putting a div inside bold tags, but it will stop you from forgetting to close tags, etc.
Perhaps designers have improved (or i've had bad experiences with them)...
Ambush Commander wrote: XSLT is nice because there's no need for a mock: you just supply a sample XML file to transform from.
I don't know how you define 'mock', but i would say that 'sample XML file' would be included ;)
User avatar
kyberfabrikken
Forum Commoner
Posts: 84
Joined: Tue Jul 20, 2004 10:27 am

Post by kyberfabrikken »

Ambush Commander wrote:XSLT won't stop you from putting a div inside bold tags ...
A DTD will.
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

Even though one could do anything in Smarty tags that you could do in PHP with {php}{/php}. Am I correct in that?
Yes. I think it's the worst thing they ever did. It can be disabled by turning "secure" mode on.
Perhaps designers have improved (or i've had bad experiences with them)...
I would think so. Asking them to create well formed XHTML isn't a tall order; really, most problems come from unescaped ampersands.
I don't know how you define 'mock', but i would say that 'sample XML file' would be included
When using a template system like Smarty, you need mock application code to create the necessary variables for the template and then invoke the templating system to generate the final product. XSLT forces the input data to be in the form of XML, which means that they don't even need the application to test style sheets: they just need the XML schema (and some sample files) and their XSLT file.
A DTD will.
You'd have to validate after the transformation took place, then.
Post Reply