tags:

views:

1170

answers:

17

Possible Duplicate
What's a good templating system for PHP?


What is your favorite PHP templating engine? There are a few frameworks, such as Symfony, that comes with its own templating engine, but there is also some other third party templating engines available, such as smarty.

Which is your favorite?

A: 

I make an engine my own, using ob_start() etc...

It's like ASP.NET Master Pages

Time Machine
+2  A: 

One vote for Smarty here as the best of the worst. Its syntax is fairly simple and designer-friendly, though I disagree with the design decision of allowing for variable assignment in templates.

cpharmston
+8  A: 

We've used PHP itself as a templating language for some time recently since it's the default method Zend Framework suggests. Lots of people advocate this for speed and simplicity, but unless you're really strict with yourself it is far too easy to put lots of business logic into the templates making them complex and unwieldily.

I'd vote for Smarty myself, though it too can give too much power in a template. It has an excellent compiling system which stores a cached version of the pure PHP version of the template to speed things up. This overcomes the issues people have with the extra processing all custom templating systems incur.

simonrjones
I use PHP itself along with a dose of self-discipline.
Rafe
Perfect example of a language giving you enough rope to hang yourself.
cpharmston
A: 

Well.

I vote for smarty as well. Small footprint and, obviously, better than rolling your own or (gasp) using echo :P

shylent
A: 

I dont know what all your requirements, but I am honestly telling you, compare some of the framework, the following link will help you.

if better dont struck only on any template engine, choose according your project nature

http://www.phpit.net/demo/framework%20comparison/chart.php

http://en.wikipedia.org/wiki/Comparison_of_web_application_frameworks

coderex
A: 

Stay away from templating engines!

Learn how to manipulate the DOM yourself, and make as clean a break as you can between business logic and controller / view logic.

If anything abstract the DOM to make helpers and such.

Phillip Jacobs
please elaborate on your philosophy
orlandu63
So, templates engines; for the most part are intrusive. Once the UI guy hands off the html in your direction, you would typically have to implement the template engine for that block of html, alot of the time that means mixing php and html. This causes all sorts of issues where the UI guy can't make changes or work with your dirty version.A better solution is to leave the html as is, bring it into the DOM, and perform any changes (controller logic) by manipulating the DOM object itself.
Phillip Jacobs
A: 

I found EasyTemplate to be useful because it uses html comment tags as placeholder and just consists of 2 files. Lightweight and get the job done. It also made it easier to customise to your needs.

<div id='main'>
<!-- welcome_msg -->
<!-- news -->
</div>

Then

$template->replaceTag("welcome_msg", $todayWelcomeMsg);
$template->replaceTag("news", $todayNews);

You can also define the template in a string, not necessary to have it in a standalone file. And templates are just vanilla html files.

Extrakun
A: 

My vote goes to the Smarty.

adatapost
+2  A: 

While PHP is a template engine itself (remember "<?php" in almost every php file?), I don't think it is rational to build template engine on top of another one...

On the other hand, having just plain PHP code for output generation forces you to reveal lot of variables (implying that OOP is used) to use them in templates.

In my opinion, template engine should provide these features:

  • fast
  • easy to use
  • encapsulation for variables
  • helpers

I would say, that Smarty and similar template engines fulfil all these requirements except ease of use. They introduce new language/syntax and limitations too.

Personally I use Zend_View component (together with other Zend components) of Zend Framework. This template engine provides object oriented implementation of PHP language based template engine. And by PHP language based template engine I mean that template files are ordinary PHP files (Zend_View component presents template as object that accepts variables and configuration and template file uses $this->varname syntax to extract variables and similar syntax to call helpers). I would say, that Zend_View implements all features I mentioned above. While I wrote a lot about Zend_View, I'm not stating that it is the best of this type of engines. There are other similar template engines with different properties (lightweight, different architecture, etc.) and you just have to find that suits you best.

Jonas
+2  A: 

My vote goes to PHPTAL (http://phptal.org/). Its most useful feature is that you can include example content in a template and leave it in. That is, you have a single file that can be used as initial mockup and as production-ready template at. I have used it in some sites, the biggest was http://sputnik.de where we used it on top of the PHP-based CMS SixCMS.

Olav
Wow, phptal looks interesting.. but how about the speed?
DaNieL
Speed of PHPTAL is pretty good - templates are compiled to PHP. The heavy DOM-lifting is done only once and cached.
porneL
A: 

Another more recent contender is the PHP version of JSON template: http://code.google.com/p/json-template/source/browse/trunk/php/jsontemplate.php. It is really small and declarative. Other implementations are for Javascript and Python.

Olav
+1  A: 

The best and most powerfull template engine is PHP itself.. but if you'd like to use a template-engine, use Smarty for it's caching functions, also it converts the templateing-code to PHP itself.

Stefan
A: 

There's a good explanation about why you don't need template engines in this post.

David Lin
A: 

I rolled my own, based on my experiences with .Net's string formatter. My Template() function takes a string with {{ }}-enclosed numbers, which map to indices in the array that is the second parameter. I like it.

Kawa
I should use it more often, come to think of it...
Kawa
A: 

I can't recommend Tiny But Strong enough. It's simply superb, especially if you work with web designers who use Dreamweaver (or use Dreamweaver yourself).

I've been using it for 3 or 4 years now and it hits all my sweetspots - straightforward, wysiwyg templates, efficient, lightweight, flexible, under active development and well documented. In fact it's the principal reason why I still do some sites in PHP rather than completely moving to Python which I prefer as a language - it's just that good.

There's a short article on their site which discusses how TBS differs from what they term 1st style templates (PHP itself) and 2nd style templates (Smarty and the like). The authors are French and the english is a little off (not that the actual documentation is anything but crystal clear) but it's worth copying the section about TBS here.

The goal is to simplify more the previous approach, by having specific tags no more coded but compatible with the nature of the template. As far as we know, TinyButStrong is the only PHP template engine which enables wysiwyg templates. Using the trick of relative tags, you can define inside zones on the visual part. For example : [b;block=tr] defines a zone named "b" and limited by the that surrounds it. Thus, templates files don't need coded loops or any procedural syntax inside, they can be created by authoring tools and can be w3c valid. Previewing templates in browsers are figuring something out. This kind of template gives more possibilities : Open Office, Excel, ... TinyButStrong doesn't work by preassigning data, it merges them on demand or sometimes automatically. Keeping the idea of being simple, the engine is not a framework or a language but just a brick in your application. It's only one file, one class with 6 methods and 5 properties. It also accepts plug-ins. It can do as much as serious template engines and is already widely used.

The part about the template system being a brick is very true. My personal default stack with it uses XAJAX and Mootools, but you can mix and match much more easily than you would with a full blown framework - not that it won't play nicely with virtually any framework you wish to throw at it. Some of the things you get for almost free too are wonderful - like the ability to direct output to excel reports rather than a webpage (using a simple plugin).

Check out their Testimonials and Sites pages.

Cruachan
A: 

I wrote http://sourceforge.net/projects/picotemplates/ and have been using it for my own projects for an year now , I don't feel like needing anything else.

It's very simple, compiles the templates to php code (so you have same speed as using php itseelf) but it doesn't have all the features of smarty (from my point of view, all 'instructions' you need in a view (template) are variable output, for and if).

cobru
A: 

I use vlibTemplate personally. It looks to be a complete clone of Perl's HTML::Template CPAN module. It doesn't allow any php code at all in the template, and is a dream to use.

Example:

select.tmpl:
<html>
  <body>
    <h1>Select a user</h1>
    <select name="users">
      <loop users>
        <option value="<var id>"><var text></option>
      </loop>
    </select>
  </body>
</html>

select.php
<?php
require_once 'vlibTemplate.php';
$t = new vlibTemplate('select.tmpl');
$q = mysql_query('SELECT id, text from...');
$t->setdbloop('users', $q);
$t->pparse();
?>
sdellysse