Page 1 of 1

Why Do We Use Smarty?

Posted: Fri Jun 06, 2008 11:45 am
by supermike
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:

Code: Select all

require_once('SMARTY.php');
2. Now we start populating variables that get passed to Smarty, like:

Code: Select all

$smarty->assign('bigtable',$sTable);
3. And then we draw it on the screen by loading our XHTML template like:

Code: Select all

$smarty->display('report.tpl');
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:

Code: Select all

$sDisp_Table = '<TABLE><TR><TD>blah...........</TABLE>';
...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:

Code: Select all

require_once('templates/report.php');
4. Inside our template, we can then use ordinary PHP tags like the following to display it in the appropriate part of our XHTML:

Code: Select all

<?= $sDisp_Table ?>
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.

Re: Why Do We Use Smarty?

Posted: Fri Jun 06, 2008 12:42 pm
by jmut
Most people against smarty that I know have two arguments: slow , learning another language.
Learning smarty takes a week at most to learn profitiantly with making own plugins, functions and whatever so I don't really consider learning another lang that much of an argument...
Slow can be again argued over I guess....as smarty provides some caching mechanisms that can be very handy and easy to use...in regular use of course it is somehow slow..cause it's another layer on top..... and here are some benefits I see with this layer:
- ready handly functions, plugins, pre/post buffer filtering for view etc, caching
- nice to provide as template engine for CMS systems or the like for customer to build own pages...but at same time restrict it..so he cannot do something stupid with full php functionality.
- being not an issue in my point that it's a new language to learn...I think smarty have shorter syntax too {$foo} is definately something I like about it.

And here...speaking of frameworks...in another topic of yours, a good framework will transperantly allow you to use whatever view engine you like, pure php, smarty or whatever...guess what... Zend Framework do that for you :)

Re: Why Do We Use Smarty?

Posted: Fri Jun 06, 2008 1:37 pm
by supermike
You know, if someone makes a Smarty-like template engine out of plain old PHP and PHP5 functions, then people could build plugins all the time for it. For instance:

Code: Select all

<?php 
require_once('php_template_engine'); 
$plugin->FilterAscii($foo);
?>
<XHTML GOES HERE>
<?=$foo?>
</ENDING XHTML>
I see your point on {$foo} versus <?=$foo?>, but I think I can live with the extra 3 characters per parameter to get the increased flexibility.

Re: Why Do We Use Smarty?

Posted: Fri Jun 06, 2008 6:37 pm
by Kieran Huggins
A friend of mine named Hampton wrote a kick-ass template language called HAML. It's natively written in (and for use with) Ruby (Rails, Merb) but there's a PHP port as well.

While I've never used the PHP port, it seems to be pretty straight forward.

HAML is just beautiful, I strongly encourage everyone to try it.

Re: Why Do We Use Smarty?

Posted: Fri Jun 13, 2008 10:15 am
by RobertGonzalez
Isn't a similar, template based discussion already in action in Theory and Design?

Re: Why Do We Use Smarty?

Posted: Fri Jun 13, 2008 11:44 am
by Kieran Huggins
yep. we doubled down.

Re: Why Do We Use Smarty?

Posted: Fri Jun 13, 2008 2:24 pm
by Zoxive
I still don't get why there are template engines like smarty and the like, when php itself is a template language.

http://php.net/alternative_syntax

Re: Why Do We Use Smarty?

Posted: Fri Jun 13, 2008 3:27 pm
by Christopher
I have said this before, I use HTML templates when untrusted users need to be able to edit them. Giving untrusted users access to PHP templates is giving them access to your server.

Re: Why Do We Use Smarty?

Posted: Fri Jun 13, 2008 4:22 pm
by alex.barylski
Different solutions for different situations.

A template language which was WYSIWYG friendly would be handy in a web design studio, but for my personal projects unnessecary.