views:

1746

answers:

18

We are developing a huge website, it will get lots of traffic, right now we are analyzing our options and Smarty looks nice but i have seen lots of flames about this, some love it some hate it. What do you think? Any real life experience with Smarty? If you hate it please write the reasons, same thing if you love it. ;)

Advice about alternatives are welcome.

+4  A: 

I've used Smarty for quite a few years now. I'm not attached to Smarty, in and of itself, but appreciate the separation of the view from the other code. Even working on projects by myself, I've found that it has given me a lot more discipline.

When I'm working on projects with a team that includes a dedicated designer, I've found that it is immensely helpful for the designer to be able to modify templates easily on his or her own.

jonstjohn
+3  A: 

I loved it. I never came across any use cases it didn't cover, and I appreciated what I felt was a very clean syntax. The documentation (http://smarty.net/manual/en/) was good too. I didn't massively like the debugging stuff, but then debugging PHP is never fun.

Dominic Rodger
+5  A: 

pros:

  • some designers/templaters know it, and can even implement some logic w/o knowing php

cons:

  • sometimes slow
  • cannot do everything php can

alternatives:

  • zend framework's mvc by default uses phtml views, offering full php functionality on demand. nearly any templater/designer skill level "supported" here.
Karsten
+5  A: 

I really don't see the point. Any MVC framework will give you much better separation of view from logic. And they give you much more flexible cache support. With MVC framework you can cache whole pages, partial views, objects, database results. Thus much more apt for hight traffic website.

And one more thing. Smarty is not PHP, so it's a new syntax that your application developer has to learn. On the other hand I don't think there are lot of ppl, that know Smarty, but don't know PHP.

vartec
+39  A: 

Personally, I've never understood why people feel the need to put a templating language on top of PHP. PHP is already a templating language. Just have some discipline/standards about what you're allowed to use on the "view" pages, and I can't see any reason to add another layer on top of things. I don't like to use anything more complex than if/else-type constructs and simple loops. If there's any processing that needs to be done, it's done before I start outputting anything.

Chad Birch
Comes down to discipline. work with a team of more than a couple of people and before long queries and strange function calls find their way into your templates. so, long as you are ready to open a can of whup-azz.
jonstjohn
Just to achieve Separation of Concerns and not to pollute the global namespace, downloading PHP Savant might be a good idea.
Andy
@jonstjohn Use version control and meet the guilty in a dark alley!
Mario
@Mario exactly, they don't call that SVN command "blame" for nothing ;-)
vartec
@jonstjohn I can put database calls in my smarty templates too... so in that sense it is no different really (you just have to surround them with {pph} {/php}). Not that I do this... But I think I will watch out for the dark alleys now...
SeanJA
+1  A: 

We have a fairly large enterprise application that we provide which is 100+ pages in size and we have used smarty on it for the past 4 years. I wouldn't recommend it, it's basically just a lame version of php. Unless you are specifically working with a design team and they will be working with the templates I would recommend staying away from it.

zodeus
+4  A: 

I'm kinda meh about it. The syntax feels cleaner, but I feel like I'm always fighting it to do things I consider very reasonable. There's a certain style of leveraging OOP that adds a lot of power and simplicity to one's code, and Smarty is actively hostile to it; usually it feels like Smarty is only grudgingly willing to allow the indirection operator (->) to exist at all. That annoys me and makes me feel like I'm coding with a hand tied behind my back for no really great reason (since the promised benefit of designers being able to edit templates has yet to come to pass for me, and designers have edited my PHP templates in the past just fine).

|escape:html, |escape:javascript and so on are a lot more pleasing than the zoo of PHP escaping functions, though. (I mean, htmlspecialchars()? Seriously, that's the best name you could think of? Wow, just wow.)

chaos
Agreed. I usually alias htmlspecialchars output to just h() to make native PHP templating much more pleasant.
bobince
+27  A: 

There is several reasons for using Smarty (or any other templating system). Here's a list from the top of my head:

  • Smarty offers you nice syntax. Opening and closing Smarty tags is like doing so with HTML tags. The only difference is {/} instead of </>
  • Smarty syntax is unobtrusive. You don't have to switch PHP and HTML context in your head, whereas using only PHP it happens a lot - all this <?php endif; ?> madness
  • Filters. This is where real power of Smarty lies. Thans to filters you answer to question what and not how. See the following example: {$fileSize|bytes} is easy to read enough to see that I'm outputting file size and apply some filter to it. Whereas in pure PHP I would be doing some if-else hakery thing missing the whole point - what I am actually outputting?
  • More filters. You might have seen on some pages info like "Last seen 3 days ago". In Smarty you could do it just by creating own filter that sits quietly in separate file and does not pollute your template. I use custom |relativeDate filter to do the trick.
  • Security. Create simple |e filter to escape output and prevent XSS attacks. How easy when compared to <?php htmlspecialchars($foo); ?>
  • Easy integration with Zend Framework. Yes, I use Smarty on top of ZF. This way I get the best from both.

The most important thing however is that Smarty lets me save a lot of time. When I look at Smarty template I can instantly see what I am doing. When I look at PHP+HTML I only see how is it done and not what, so I have to re-discover my own code. I can hardly imagine more unproductive activity.

Michał Rudnicki
Yay filters! Yay clean looking templates! Also, caching can be quite useful.
GloryFish
I use smarty simply because of the <?php echo $foo; ?> madness. If the php crowd would be less militant, I'd love to simply: <?= $foo ?> or even use asp tags: <%= %foo %> Nice and clean.
DGM
About the security point, Smarty already has a |escape filter. I personally use Smarty because I don't want the designers to start doing random PHP when they have no idea of the security concerns and implications of what they are doing. It's easy in Smarty to allow templates to do only what you allow.
Andrew Moore
Gotta diagree on the nice syntax part - The curly brace was a poor choice of delimiter - you have to escape any js with curly braces to avoid smarty parsing it.
Cory House
Cory, indeed curly braces are not the most fortunate default choice. But you can easily change it in Smarty config. I for instance tend to use {{ and }} symbols. Apart from that the syntax is actually quite readable.
Michał Rudnicki
@DGM: Not only that, but also `<?php if empty($list) { ?>...<?php } else { foreach ($list as $item) { ?>...<?php } } ?>` compared to `{foreach from=$list item=item}...{foreachelse}...{/foreach}`. Only the second one designer can understand.
Wernight
A: 

The only reason I need to not use Smarty is that it adds another layer of complexity only for the sake of restricting flexibility. You want neither of these in your software. Smarty is very feature rich. If you don't need all those features then use something simpler.

Here is a more general question (What’s the best way to separate PHP Code and HTML?) about the same problem that references Smarty.

gradbot
+11  A: 

I've been using Smarty for around 5 years now, and I'm rather fond of it for several reasons:

  1. It's sheer elegance - it's much more gratifying to look at a smarty template that's been written some time in the past and continued to function correctly despite changes you may have made to the application (or model & controller in the case of MVC apps), than intermingled php & html classic 'index.php' style development with include statements everywhere. As long as the data supplied to your smarty templates during assignment is of the correct structure, everything should work as you expect it to.

  2. Coming from a JSP background, it's clear that it has been influenced by the JavaServer Tag Libraries (JSTL) which always made sense to me as a means of separating implementation from presentation - and for that matter, it's syntactical consistency with other templating engines.

  3. It is fairly light-weight. I have read many critical statements on how badly it performs in comparison to straight PHP calls. Shocking! Who would have thought an extra layer of software could slow things down?

  4. It is well-maintained and extremely well documented. Until recently, it had it's home on php.net. The documentation can be read through and understood in about an hour. Examples provided per function are so well thought out one can simply copy and paste and change the variable names in many situations.

  5. It can be by-passed - in those odd situations where small numbers of milliseconds make a big difference, don't use Smarty, use pure-PHP instead. Smarty does not have to be switched off, just don't use it!

  6. Smarty is great for AJAXy web apps - create a main page template (your index.tpl) and your usual static implementations of each little section of the main page - e.g. header.tpl, left-nav.tpl, body.tpl, left.tpl, footer.tpl etc. These will render beautifully when the page is called via a standard GET. Need to replace the content of the main page via AJAX, use something like:

        if($request->isXmlHttpRequest()) {
            echo $smarty->fetch('rightbitofpage.tpl');
        } else {   
            $smarty->display('wholepage.tpl'); 
        }
    

    I could rant on for hours about why to use smarty and when not to use it - bottom line, I see it at it's best as being an outstanding technology, and at it's worst as being under-featured when compared to the likes of, say, pure-PHP. I can testify to one thing though, it has never 'hurt' a single application I/We have developed over the years.

Hope that helps!

karim79
+2  A: 

I'm using Smarty and YES - it is best choice for me now.

Mr.ElectroNick
A: 

If what you're building will get a lot of traffic the caching functionality of Smarty might be a godsend. As for the syntax and stuff, try making a three page test site and see if you like it: this is highly subjective (I do, somebody else might not).

I've been using it since 2003 at least and I know exactly when I'm happiest at having used it: when maintaining old and long forgotten code. Priceless.

djn
A: 

The major advantage of a template language is not to seperate logic from output. The major advantage is to restrict the output to a defined set of possibilities. Smarty allows you to define own blocks and template-functions so you can create your own "controls".

My template for a tabbed form (including some fields with onblur- and onsubmit-validation) look similar to this:

{form id=$item->Guid action=$controller->requester->create(3)}
    {tabs}
     {tab label="Data"}
      {fieldset}
       {field label="Type"}
        {include file="core/control/input/select.tpl" id="Type" options=$controller->getTypes() selected=$item->Type default="0"}
       {/field}
       {field label="Date"}
        {include file="core/control/input/date.tpl" id="Date" value=$item->Date validate="isDate()"}
       {/field}
       {field label="Image"}
        {include file="core/control/input/image.tpl" id="Image" value=$item->Image mode="simple" validate=""}
       {/field}
       {field label="Title"}
        {include file="core/control/input/text.tpl" id="Name" value=$item->Name validate="min(3);max(255)"}
       {/field}
       {field label="Description"}
        {include file="core/control/input/textarea.tpl" id="Body" value=$item->Body validate="notEmpty()"}
       {/field}
       {field label="Is Online?"}
        {include file="core/control/input/checkbox.tpl" id="IsOnline" value="1" checked=$item->IsOnline}
       {/field}
      {/fieldset}
     {/tab}
     {include file="core/shared/form/tab.metadata.tpl"}
     {include file="core/shared/form/tab.history.tpl"}
     {include file="core/shared/form/tab.actions.tpl" id=$item->Guid}
    {/tabs}
    {include file="core/module/form/action.tpl" id=$item->Guid}
{/form}
Joe Scylla
Quite possibly the ugliest thing I have seen
SeanJA
A: 

Calypso is a php clone of the Django Templating Language. A key design feature of the dtl is that context data is imutable once it get's it's way into the template. Essentially it forces you to separate your template (presentation) code from the controller/model. It's pretty intuitive, extendable and easy for designers to deal with.

Whatever decent template system you use it won't affect the performance of your site to a noticeable degree, especially if you are querying lots of tables with lot's of joins etc. Once your templates are developed you can use memcached to cache the template code instead of reading them from hdd with every request.

Vasil
+1  A: 

I prefer to use 'Dwoo', it's just as easy as Smarty but there are more possibilities in Dwoo which aren't supported in Smarty.

Besides that, it's easy to implement it in Zend Framework. And Dwoo has a very good documentation

Polichism
A: 

I also want to underline the great support to gettext

Put something like that in your template: {t}word{/t} prepare the .po file and you are done: you have a multi-language web site. [ok it's a bit more tricky than this but after the first setup you can forget about it]

http://smarty.incutio.com/?page=SmartyGettext

dam
A: 

I just spent the last two hours trying to make a template (a foreach, and two ifs) that should have taken 5 minutes. Isn't PHP supposed to be a templating engine ?! I urge you not to use it. Choose a good PHP framework, most of them have a templating library that you can use.

Vlad
+2  A: 

I have created several Smarty based application frameworks and they have been used to create hundreds (no kidding) of web applications by my company and my customers. I have trained several groups of programmers to PHP and after showing them Smarty I have always heard the question: "Why didn't you tell about this before?"

As for Vlad's comment "choose a good PHP framework... most of them ha ve a templating library" (well why not Smarty, then) and "...spent two hours trying to make a template". Sigh. The documentation is there, just read it. Understand it. The syntax is extremely easy. Well, one cannot really expect to learn a new environment in two hours, though and become 100% productive.

There are other good alternatives as well like Open Power Template http://www.invenzzia.org/en/home or Twig http://www.twig-project.org/development, the latter is lacking many of Smarty's advanced features, though.

Anyhow, I would never-ever start any project of any size without using a good templating system.

I have done my best to try to understand the Smarty-haters' motives but failed so far. Once I had to work for a company that refused to use Smarty 'because of performance reasons' so they created a 'more powerful' regex-based template system. Well, in complex reports their 'speedy' system took 8x the CPU time my Smarty based alternative did - and this without even optimizing the Smarty end or using bytecode cache. Moreover, we had to write much of the formatting code at the PHP end which made the code much more unreadable and harder to maintain.

karvonen
Smarty-haters are not regexp-users. Their motive is "Why to invent a PHP written in PHP"?
Col. Shrapnel