Why Do We Use Smarty?

Ye' old general discussion board. Basically, for everything that isn't covered elsewhere. Come here to shoot the breeze, shoot your mouth off, or whatever suits your fancy.
This forum is not for asking programming related questions.

Moderator: General Moderators

Post Reply
supermike
Forum Contributor
Posts: 193
Joined: Tue Feb 28, 2006 8:30 pm
Location: Somewhere in the Desert, USA

Why Do We Use Smarty?

Post 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.
jmut
Forum Regular
Posts: 945
Joined: Tue Jul 05, 2005 3:54 am
Location: Sofia, Bulgaria
Contact:

Re: Why Do We Use Smarty?

Post 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 :)
supermike
Forum Contributor
Posts: 193
Joined: Tue Feb 28, 2006 8:30 pm
Location: Somewhere in the Desert, USA

Re: Why Do We Use Smarty?

Post 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.
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Re: Why Do We Use Smarty?

Post 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.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: Why Do We Use Smarty?

Post by RobertGonzalez »

Isn't a similar, template based discussion already in action in Theory and Design?
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Re: Why Do We Use Smarty?

Post by Kieran Huggins »

yep. we doubled down.
User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

Re: Why Do We Use Smarty?

Post 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
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Why Do We Use Smarty?

Post 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.
(#10850)
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: Why Do We Use Smarty?

Post 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.
Post Reply