5

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.

2

9 Answers 9

14

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.

1
  • 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. Oct 13, 2008 at 17:24
12

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.

2
  • It is also great for generating email messages in both plain text and HTML. Oct 13, 2008 at 12:58
  • That's an excellent example that I hadn't considered.
    – Mark Biek
    Oct 13, 2008 at 12:58
3

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.

2
  • What does this have to do with template engines other than he doesn't use one?
    – rick
    Mar 29, 2009 at 21:07
  • 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
    Mar 29, 2009 at 21:09
3

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.

5
  • 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
    Mar 29, 2009 at 19:59
  • 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
    Mar 29, 2009 at 20:35
  • @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).
    – Kornel
    Mar 29, 2009 at 20:45
  • @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().
    – Kornel
    Mar 29, 2009 at 20:48
  • 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
    Mar 29, 2009 at 21:00
1

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.

1
  • 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. Oct 13, 2008 at 12:57
1

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

0

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.

0

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.

6
  • How do you accomplish this exactly?
    – Kzqai
    Jul 21, 2011 at 15:43
  • The simple answer: in your PHP file, you include anoter PHP file which includes the HTML and possibly some simple PHP. And anytime you are generating HTML inside the PHP file, try to move it to HTML (but only when it makes sense, without making things too complicated). Let me know if this answers your question or if you'd like an example. Jul 21, 2011 at 16:46
  • Right, so you're talking about -simple- php scripts that do have some php syntax, not pure html files at all. I was wondering if you were referring to a system like clojure's enlive ( stackoverflow.com/questions/3248235/… which uses css selectors on pure html to recompose it) for the php language, since I've been looking into the possibilities for that.
    – Kzqai
    Jul 21, 2011 at 17:04
  • I prefer to use what's built into PHP instead of creating something else that needs extra processing on every page request. Jul 21, 2011 at 19:48
  • Just makes for dirtier templates, generally.
    – Kzqai
    Jul 21, 2011 at 20:46
0

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.

Not the answer you're looking for? Browse other questions tagged or ask your own question.