views:

2025

answers:

9

I found that using Smarty with PHP, sometimes extra time will need to be used for

1) using quite different syntax than PHP itself
2) need to check small cases, because documentation doesn't give finer details, such as for "escape"

http://www.smarty.net/manual/en/language.modifier.escape.php

it doesn't say escape:"quotes" is for double quotes only or for single quotes as well, so you need to write code to test it. Also for the case of escape:"javascript" -- can't tell exactly what and how it is escaped.

3) for something complicated, need to write helper functions or modifiers, so it needs a creation of new files and ending up doing it in PHP again.

by the way, does using Smarty provide a good speed up over use PHP alone? thanks.

+1  A: 

Using Smarty as a templating engine will probably not be as performant as not using it, as it is an extra layer of software, i.e., a templating language on top of a, erm, another templating language. On the other hand, if you use the caching feature properly you could realise overall performance gains.

Smarty templates are pre-compiled before being output to the browser, which involves writing temporary files to disk. This step surely penalises performance, at least a little.

If you are confident in your ability to keep implementation and presentation separate, and have no real interest in server-side caching, you should probably just use pure php templating. Some MVC frameworks such as Zend Framework have their own PHP-like templating systems.

On the other hand, smarty is a decent way to enforce a neat separation of presentation from implementation, particularly where it's unclear as to what belongs where. It might help discipline you to enforce this necessary separation.

That said, I use Smarty in most of my PHP projects, as it reminds me of Java-Server Tag Libraries (JSTL), which I'm very, very used to, and fond of.

karim79
+1  A: 

As far as I know Smarty is one of the best template engines speed wise. Maybe it takes a while to get used to it. But if you are not working on the system alone and the amount of html and style files is huge it speeds up the development significantly.

While working on my last project the design was changes a couple of times but logics was the same. I guess that's the best example when Smarty or any other template engine helps a lot.

dasha salo
+1  A: 

Personally I use Blitz for templating. On the site the author claims it is the fastest templating engine and provides a (biased?) chart over performance between different templating systems for PHP. I haven't used smarty myself, but this may give you some hints on its performance.

http://alexeyrybak.com/blitz/blitz_en.html

antennen
+2  A: 

I like template engines and think they should be used, but in the particular case of Smarty, I think it's waste of time, because it's not a significant improvement over PHP as templating language:

  • The new syntax is still based around old concept of special tags inserted in random places in the document.
  • Because Smarty does not understand HTML's syntax/structure, it cannot not help you to create valid/well-formed HTML. Smarty's tags violate HTML's syntax, so once you add them, other standard tools can't help you either.
  • Smarty's output, just like in PHP, is insecure (unescaped) by default and you have to remember to add |escape everywhere where you output data in HTML.

There's one particular PHP template engine that I've fallen in love with, which fixes all those problems: PHPTAL.

It's still something new you have to learn, and it's a depenency for your application, but I think having XSS and ill-formedness problems solved makes it worth the trouble.

PHPTAL just like Smarty is compiled once to PHP and cached, so performance is comparable to raw PHP.

porneL
the one thing i loved about smarty were the filters, to auto-escape output. You don't find that in many other systems.
I.devries
@Vatos: IMHO that is a flaw, because you have to use that filter. If you forget, your output will be insecure/invalid. Proper template system shoudl auto-escape automatically without needing special filter.
porneL
+2  A: 

Smarty is certainly one of the best template engines out there. In my experience though people would be well advised to think their use cases through more thoroughly before they use any templating engine on top of PHP at all.

First, PHP itself is perfect for templates. Pretty much the only justification for using another templating engine is if you're allowing untrusted users to create or edit templates, since they could execute all kinds of badness. So, if your project has user-editable templates, use Smarty. If not, stick with PHP.

If your problem is separation of code and layout, I suggest you look into implementing a lightweight MVC-style execution model. Or, to put it more condescendingly, if you have deeper logic code in your template, it's probably time to do some refactoring.

Performance is another consideration. Yes, rendering a Smarty template comes with a cost. But after it's done, the output should be cached, leading you to improved execution times. Same goes for PHP templates. PHP allows you to implement all kinds of granular caching models through the use of its output buffers. But beware of premature optimization: do that only after you're code complete and have identified what the actual bottlenecks are!

The biggest cost when using Smarty or any other engine comes in the form of developer time. It's another layer of complexity and inevitably you will find yourself in situations where you have to trick the engine into doing what you could have accomplished within pure PHP all along.

Udo
+11  A: 

First, PHP is a templating language. Keep that in mind when you talk about using a template system for your PHP-based web applications.

The only 'real' argument that I've ever heard for using ANY templating engine was that they provide a simpler language for template manipulation which can be handy if you have template designers who don't know PHP and whom you don't trust to learn to use PHP judiciously.

Regarding these arguments, I would argue that if your template designers are not competent to learn enough PHP for template design, you should probably consider finding new template designers. Additionally, PHP itself provides a different syntax for control statements that you might use in a template versus in code. For example:

<? foreach($array as $key => $val): ?>
    <?= $val ?>
<? endforeach; ?>

VS:

<?php
    foreach($array as $key => $val) {
     echo $val;
    }

?>

Personally, I believe that templating engines arose in PHP because:

  1. That's way that other languages do it
  2. Better PHP programmers realized that they needed a way to enforce separation between presentation and application logic and templates were an easy way to do this.

The first reason is just kinda silly. The second reason can be overcome with a little self-control and even a rudimentary understanding of the necessity of separating layers in an application. The MVC design pattern is one way of approaching this problem. As far as exercising some self-control, my rule is that only necessary loops and if statements get used as well as functions that filter, escape, format the output for the screen.

Having used Smarty extensively, I can honestly say that it always presented me with more hurdles to get over than it did solutions. If anything, switching to PHP-based templates is what has actually decreased development time for both templates and code.

Noah Goodrich
+1 for truth. BTW: you have an error in your code: $val++ should be echo $val to match the first code example.
Residuum
@Residuum - Fixed.
Noah Goodrich
Yet they added the {PHP}{/PHP} tags so I can do all of my database queries there ;)
SeanJA
Only you forgot to mention that you can't rely on short syntax as it very well could be disabled. So if you are developing serious project that runs not only on your machine then you would have to flood your page with `<?php echo $var ?>`. Now that doesn't look so sexy comparing to `{$val}` does it?
serg
+3  A: 

I don't like templating engines. I find them very lossy and resource-intensive for PHP.

With MediaWiki, around version 1.6.x we backed off using Smarty by default and just use PHP's built-in templating, with a great improvement in performance.

I've found that most of what people want to do with a templating system (add links, change colors, remove text or sections of the page) are better done with a simple system of event hooks.

Laconica, the open microblogging platform, doesn't do any templating by default. We have a plugin for people who are crazy for templating.

Evan P.
+1  A: 

Pros

  • No PHP in your HTML files (Allows both PHP and HTML identing)
  • Pipes {$var|default:"None selected"} {$var|urlencode}
  • Foreachelse: {foreach item=row from=$results}{$row.name}<br>{foreachelse}No results{/foreach}
  • Themable websites/pages (Using only CSS has it limits)

Cons

  • Other language syntax
  • Not always obvious code {"Y-m-d"|strftime:$timestamp} {$array|@var_dump}
  • Slight overhead

I can highly recommend the "template" approach(mVc), but both Smarty and plain PHP are up for the task.

Bob Fanger
A: 

Why use a template engine when you can just use your html files and inject php code where you need it? you can do this with Psttt! templating engine for php

full source code here http://github.com/givanz/psttt

codeassembly