Smarty Best Practices 1

I know I have slagged a lot on smarty in the past, both on my blog and on twitter… It is not because I do not understand it, it is because I see it being misused (at least as far as I am concerned, I may be wrong about it) every day in old projects and all over the internet. So, to make up for the slagging that it has received from me, I will now attempt to show you some best practices (or How to use Smarty the smart way). So, without further ado…

Rule 1: Don’t Manipulate the data

First of all, before you even start using smarty, you have to realise that this is a templating language. You are not supposed to process the data, create new data, delete data, get new data (and so on)… When you are ready to display your template you should already have the data retrieved/created, and ready to go. If you are considering doing this in your smarty template, stop and take a look at your backend code, you probably did something wrong back there. I guess I should do these as some kind of list, so…

Sure it is tempting to do:

{assign var='newVar' value=$oldVar.varThatINeed}

But don’t. It means that people have to go looking for not only $newVar in the php code that displayed the template, they also have to go looking through the smarty code for the assignment. If you need to access that variable, do this instead:

{$oldVar.varThatINeed}

If that variable is supposed to get something from an array, do this:

{$arrayVar[$oldVar.varThatINeed]}

Sure it takes a second more to write it out each time, but it is easier to find in the smarty code.

Breaking Rule 1

There are times when you might need to break rule one. If you find yourself doing anything more complicated than:

{math equation="x + y" x=$height y=$width}

in your template, you should take a step back and look at your code again, why are you doing it this way? Should you be doing it this way? Isn’t there a better way?
I think that even this example provided by the smarty documentation is probably a bit much for the template, especially if the equation is likely to change:

{* Calculate something *}
{math equation="(( x + y ) / z )" x=$vars.var1 y=$vars.var2 z=$vars.var3}

It should probably be something more like:

function calculateSomething($vars){
...
}
//notice I don't rename the function so that you can find it easily later on
//if you didn't want your function named this way, name it something else in your php code...
$smarty->register_modifier('calculateSomething', 'calculateSomething');
$smarty->display('my_template.tpl');
{vars|@caclulateSomething}

Notice the @ symbol, this means pass the whole array as one variable. This is important because smarty apply the modifier to each of the variables in the array if you do not (unless that is what you wanted to happen). While in some cases you may need to break the first rule, if you do it this way, it is managable, reusable. An even better way would be to do this before it gets to the template, but it is understandable to no want to go through the data twice, so this seems like a fair trade off as you are still doing the manipulation in php, and displaying the results of the manipulation in smarty.


flattr this!

Leave a Reply