Why Do We Use Smarty?
Posted: Fri Jun 06, 2008 11:45 am
A PHP dev friend and I were discussing why we are using Smarty and have decided to stop using it, and here's why.
When you look at our Smarty files, they're not pure XHTML with just a small smidgen of Smarty tags inside. They're XHTML with a lot of Smarty tags inside. So, this presents two problems. One, a new developer needs to learn the Smarty lingo and the dos and don'ts of it as well (which unfortunately they learn later or by trial and error). Two, if you're trying to get away from spaghetti code of logic and XHTML mixed together, it defeats the purpose.
Next, let's take a look at the process of how you use Smarty typically.
1. First, you instantiate your personal Smarty class, as I do here:
2. Now we start populating variables that get passed to Smarty, like:
3. And then we draw it on the screen by loading our XHTML template like:
4. Inside our template, we can then use Smarty tags to pull 'bigtable' and display it in the appropriate part of our XHTML.
So then you have to ask yourself how this isn't different from doing this:
1. You don't need to instantiate any class. It's PHP and can do just fine on its own.
2. Now we start assigning variables, like:
...where the "Disp_" is a visual cue that indicates that this variable is going to be displayed in the browser page.
3. And then we draw it on the screen by loading our XHTML template like:
4. Inside our template, we can then use ordinary PHP tags like the following to display it in the appropriate part of our XHTML:
So, Smarty is redundant to what PHP offers in my opinion. It's just that we need to rethink how we use PHP so that we try to mix XHTML and PHP less. By sticking your XHTML templates into ordinary PHP files in a separate templates folder, and by trying as much as possible to reduce the need to do loops and complex if/then logic inside those template files and trying to stick with mostly what you see in step #4, you make for cleaner templates that other programmers can extend easily without having to learn Smarty.
And, it might actually run faster than the Smarty technique.
That's my two cents.
When you look at our Smarty files, they're not pure XHTML with just a small smidgen of Smarty tags inside. They're XHTML with a lot of Smarty tags inside. So, this presents two problems. One, a new developer needs to learn the Smarty lingo and the dos and don'ts of it as well (which unfortunately they learn later or by trial and error). Two, if you're trying to get away from spaghetti code of logic and XHTML mixed together, it defeats the purpose.
Next, let's take a look at the process of how you use Smarty typically.
1. First, you instantiate your personal Smarty class, as I do here:
Code: Select all
require_once('SMARTY.php');Code: Select all
$smarty->assign('bigtable',$sTable);Code: Select all
$smarty->display('report.tpl');So then you have to ask yourself how this isn't different from doing this:
1. You don't need to instantiate any class. It's PHP and can do just fine on its own.
2. Now we start assigning variables, like:
Code: Select all
$sDisp_Table = '<TABLE><TR><TD>blah...........</TABLE>';3. And then we draw it on the screen by loading our XHTML template like:
Code: Select all
require_once('templates/report.php');Code: Select all
<?= $sDisp_Table ?>And, it might actually run faster than the Smarty technique.
That's my two cents.