tags:

views:

549

answers:

10

What's the best/easiest to integrate templating system for PHP, and what are the benefits of using one?

I currently don't use one at all, and am thinking that it might help to seperate content from presentation a little more.

+11  A: 

Smarty


I've found it to be fast, easy to use, and easy to install (even in a shared-hosting environment). It also doesn't require that you use validating XHTML which is handy sometimes (although I think the template engines that do require valid XHTML are probably faster.)

It's really nice to have your content in one place and code somewhere else.

Plus you're not limited to just HTML. I've used Smarty to generate XML and even SQL.

Mark Biek
It is also great for generating email messages in both plain text and HTML.
Michał Rudnicki
That's an excellent example that I hadn't considered.
Mark Biek
+1  A: 

In addition to Mark's experience, I've found Smarty well-suited for extension. I've built a (corporate) MVC framework with built-in views based on Smarty which was both easy and is flexible. The host of available template helper functions can also be extended very easily.

Konrad Rudolph
So did I. Additionally, I use friendly View wrapper for Smarty that uses indirect getters/setters to assign variable to Smarty tag. Thanks to __toString views can be easily nested within views.
Michał Rudnicki
A: 

It depends on your focus: different template engines can be better for different things. Smarty is relatively heavy compared to some, and defines a syntax that is simpler but more restricted than PHP's. There are benefits to using a syntax that is less like a programming language, for example if you're going to be asking a graphic designer to build your templates.

A template engine such as Savant3, or that used in the Zend_View components of the Zend Framework is lighter-weight and uses PHP as it's syntax. This gives you more power and flexibility in your templates, but requires you to be disciplined about keeping business logic out of the templates. Also, PHP syntax may be too complex for your designer, if he is going to be building the templates.

Richard Turner
A: 

For some more information, see this question

Jrgns
+12  A: 

PHP is a pretty good templating language by itself. Most leading PHP frameworks don't use a seperate templating langauge for just this reason.

Just make sure you use (something resemebling) MVC, and don't do any data access or business logic inside your view files.

I've also used Smarty extensively, but find it has little advantage compared to straight PHP, except for forcing you to keep your view dumb. It might also looks a little better to designers, but the down side is flexibility to you, the template implementor. Things like looping over triply-nested arrays become harder than with straight PHP.

Alexander Malfait
Alex, you're of course right. However, “keep your view dumb” (for the designer / non-programming personnel) is often precisely the reason for using a template system. Besides making it superficially easier, it also prevents (fatal) errors on the part of “stupid” users.
Konrad Rudolph
+3  A: 

To follow up on what Alex said, I suggest taking a look at Rasmus Lerdorf's article The no-framework PHP MVC framework.

Rasmus Lerdorf is the guy who created PHP.

R. Bemrose
What does this have to do with template engines other than he doesn't use one?
rick
It's not a framwork, it's a framework! The code is absolutely horrible: Html generated in code. Every controller class has to repeat the same includes, and there is no routing available since Rasmus has some illogical allergy to front controllers.
rick
A: 

I personally like the idea of just using separate PHP/HTML files. It allows me to use other HTML editors if I want (such as DreamWeaver) and can also allow people who don't know PHP, but do know HTML to edit the templates without having to learn another language.

Darryl Hein
A: 

I have used Smarty for years, but got frustrated because the special smarty syntax got in the way, where some simple php would be way easier. PHP itselves is in its very core a template language! I learned that the php api is apt enough for all presentation logic.

Savant3 and its descendant Zend_View are therefore superior solutions. Like in Smarty, you can easily write plugins for repetitive tasks.

Exception e
+2  A: 

I bet on PHPTAL.

It checks file syntax and ensures output is well-formed XML with proper escaping. This ensures that pages are safe against XSS attacks without programmer having to worry about it every single time.

Smarty and raw PHP don't handle escaping automatically, which means that every echo $foo or ${foo} (without |escape} or htmlspecialchars()) might be HTML injection vulnerability or at least break well-formedness.

PHPTAL has nice syntax that fits how HTML/XML work, e.g. you don't have to repeat yourself when you want to conditionally wrap something in a tag:

<strong tal:omit-tag="condition">
  xxx
</strong>

rather than:

{if condition}<strong>{/if}
  xxx
{if condition-again!}</strong>{/if}

And the syntax is XML without being overly verbose (mostly only attributes are added). There's no ugly mix of HTML and custom tag-like constructs.

porneL
to each his, own, I think that tal:omit-tag is ugly. Plus, if there is a lot of data between the strong tags, it may not be obvious when you get to the closing tag that it may not always be there.
DGM
I gave up on phptal for 3 reasons: Including partial templates with the "metal:" namespace was confusing and ugly. TAL tags can get verbose and confusing for more complex view logic. Creating custom modifiers was painful.
rick
@DGM: you could forget that it's optional, but it won't have as big consequences as forgetting about it in other engines (which will let you have out-of-sync opening and closing tag, which is never a good thing).
porneL
@rick: I must agree about metal: - XML doesn't allow common pattren of having body opened in header file and closed in footer. However custom modifiers can be done using function phptal_tales_modifiername().
porneL
I tried to use "phptal_tales_modifiername()" but I don't like global functions, it allows only one parameter, I didn't understand "nothrow" and the need to call "phptal_tales($src, $nothrow)" from within my modifier function.
rick
A: 

Perhaps the most compelling reason to use a template engine is default output escaping which can reduce or eliminate xss vulnerabilites.

rick