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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
Perhaps the most compelling reason to use a template engine is default output escaping which can reduce or eliminate xss vulnerabilites.