Why I use Smarty: PHP is an ugly templating language
Geek, PHP Development February 5th, 2009There’s a debate going on here regarding Smarty‘s usefulness as a developer tool. It was sparked (again) by the launch (or at least newly noticed) nosmarty.net. I commented:
I don’t understand how ZF or Symfony or any framework can be an “alternative” for Smarty. Smarty is not a framework — it is simply a templating engine. It should not be used as anything other than a component of the view layer of your MVC application. Just like any other software, it has a learning curve and a certain amount of quirks. Using Smarty does not guarantee good design — that still needs to be handled by programmers who have more experience than writing a “Hello, World!” program and reading a tutorial.
I think the fact that it’s been around since 2001 and is still in use is a testament to it’s usefulness within the community. If Smarty is used in the right manner, I don’t see why it can’t have it’s place within the arsenal of tools for developers to use.
It was countered by Paul M. Jones with:
In my opinion (note the small “o”) there *is* no right manner in which to use Smarty. It is the solution to a mis-stated problem.
I suppose I shall have to expound on that at greater length in another blog post.Oh, I already have! http://paul-m-jones.com/?p=273
I understand this point, however I still have one reason to use Smarty: PHP is an ugly templating language. I’ve used Smarty and Symfony (which relies on PHP’s alternative syntax for templating) and I still prefer Smarty.
The examples on nosmarty.net generally site one instance of echoing a variable. In this small case, sure, I can see — why use a heavier templating system that takes longer to parse and can be a bitch to configure and deal with. However, if you’re looking at a portion of a html page that has to output multiple variables, Smarty is much easier on the eyes.
SMARTY:
<td>Name</td><td>{$name}</td>
<td>Email</td><td>{$email}</td>
<td>Phone</td><td>{$phone}</td>PHP:
<td>Name</td><td><?php echo $name; ?></td>
<td>Email</td><td><?php echo $email; ?></td>
<td>Phone</td><td><?php echo $phone; ?></td>
When you start repeating <?php … ?> everywhere it gets fairly redundant, and, frankly, ugly.
Perhaps my use case for Smarty is a bit selfish, but, ultimately, programming style and comfort boils down to personal preference. Most, if not all, programming tools have the ability to be misused. I still consider Smarty to be a useful tool in my development arsenal. Though I have not used smarty in over 2 years now, using strictly PHP templating (mainly due to Symfony’s templating conventions), I would happily start using it again to regain readable templates.
October 1st, 2009 at 1:32 pm
Did you know that you can use
<?= $var ?>
to echo a variable? That is the same as<?php echo $var ?>
.December 22nd, 2009 at 11:52 am
Yup. The problem is that you can’t be sure that short tags are universally available. Not a particular problem if you’re only writing software for your own systems, but I think it’s a bad habit to get in to. I always use the full <?php tag
March 31st, 2010 at 9:38 am
I agree with your article, I’ve been using Smarty for 3 or 4 years and still love it. Mixing PHP and HTML makes for some serious ugly code.
As for I also find that very ugly, and like the author I always type the full <?php part in the tag because back in my Dreamweaver days I was quite annoyed by people who didn't (if it's just <? then Dreamweaver doesn't recognize it as PHP, and I'm sure other IDEs have this fault too).
July 12th, 2010 at 2:58 pm
Actually, the examples on nosmarty are much more extensive than that, and detail smarty’s terrible syntax for variable assignment and function calls.
I recommend using H20 templates or even better, just use Django and you’re better of in every imaginable way.
November 18th, 2010 at 1:46 pm
nosmarty site is outdated and innaccurate. its just propaganda. think for yourself.
November 18th, 2010 at 5:10 pm
I’ve been having a discussion about Smarty lately also. I told them one thing: Meta-data. You don’t have ANY Meta-Data NOR skins or themes whatsoever.
JSP has _some_ but PHP is _not_ JSP. So unless you plan on going through some trouble to mimic this behavior, you should stick with Smarty.
November 24th, 2010 at 5:10 am
Smarty is the number one choice for templates. I don’t know how many hours – these so called programmers – have spent doing webpages, but I’ve done it for like 10+ years and the ugly php code just makes me vomit.
Smarty is easy, everyone can learn it and I don’t have to type my ass out to output a variable.
{$var}
SHIFT+{}, LEFT ARROW, $var – Isnt this easier to type? THAN:
SHIFT+
– Gimme a break
– Gimme a break
Every other template: Gimme a break…
I don’t like to be a robot, so redundant php tags make me going crazy.
November 28th, 2010 at 10:53 pm
Had to fix a site made with Smarty, my first experience and a bad one – it’s an admirable effort but pointless. They’re years behind MVC and anyway, I think the task is so large a small team will always play catch-up. Version 3 is horrendously buggy and anyway, replacing php tags with their own is only going to lead to a new language (IF it continues), perl verses php and so on. I wouldn’t recommend it to anyone I like.
December 1st, 2010 at 6:04 pm
Things depend. smarty just a option.
Currently developing includes html, css, js, maybe some libraries. the testing and debug is not a easy work.
using php without template (in dreamweaver) is a total WYSIWYG (that’s why dreamweaver is hot).
the purpose of php is to make developing easier and rapidly. forget the templates and mvc framework, except you use some CMS or open-source framework.
Smarty is only smart on present level template, but not smart on whole web developing at all!
December 3rd, 2010 at 7:54 am
I’m not sure that there is a tool out there I’d recommend, and that includes any of the main templating engines. My reasoning is simple: the amount of work that goes into making the engines perform has always outweighed the amount of work it takes to learn to break the construction of a good website and page into easily managed programming tasks.
For example take the typical 3 column “blog, link, and article site” with the following components: A header bar, a footer bar, a top menu, a left side menu, stacked right side boxes, and a main area where all of the links in the main page place their results. Make it even more complex by making the top menu a “tab bar menu”. Now decide to make the whole thing very dynamic and web 2.0 and how about the mobile site, the android version, and the iPhone app… Oh, and now that my CSS skills are coming up to snuff, I can really….
Do what? go back and redo all of my templates? perhaps double, triple, or quadruple the number of templates I have to use?
It’s not that I don’t believe in the separation of user interface from business logic from pure programming. It’s just that I can use nearly pure programming to build easily comprehensible components that output to pure HTML/JS-Ajax/css’d files (which is what the server sends out to the world anyway, and let CSS, etc. take care of the rest.
December 3rd, 2010 at 7:56 am
A small clarification… I don’t code the HTML in PHP, and I do use standard but high speed text replacement logic in smaller HTML sections, which means that technically I am using templating. But it only takes about four functions, and is so simple I can teach it to a new programmer in less than an hour.
December 10th, 2010 at 3:04 am
@Jesse Dhillon
Quote “Did you know that you can use to echo a variable? That is the same as .”
When working with the Zend Framework and if you follow the coding conventions, the use of short opening tag of PHP isn’t allowed.
I’ve been working with Smarty for 4 years now. I love it, but I hate it sometimes as well. Like some people already said, you should have to look at each project and see which choice is best.
The idea of Smarty is giving people who don’t have PHP skills the ability to change templates and for that it should be used.
March 24th, 2012 at 3:54 pm
Smarty is useless. Short tags in future PHP versions are on by default. So, there is NO point in using smarty, it only adds another overhead layer on top of everything…
March 26th, 2012 at 1:19 pm
I’d still recommend keeping short tags off to be more compatible with things like < ?xml. However, as of PHP 5.4.0, the short_open_tag setting won't affect the
< ?=
shorthand – that will always work.I think that templating systems will be a case of “agree to disagree” – I still see the benefits of using something other than using PHP as your view layer of an application. In fact, Fabien Potencier (initial architect of symfony), who was previously against templating engines, has changed his mind and even wrote his own. Here’s his argument: http://fabien.potencier.org/article/34/templating-engines-in-php