Page 1 of 1

Template engines: which one do you recommend?

Posted: Wed Nov 26, 2008 4:44 am
by Apollo
I'm about to decide which template engine to use for a new website. I need a lightweight yet flexible template system. It will be used for basic CMS functionality, and for some dynamic content I need to be able to include direct PHP in my pages (i.e. throughout the template pages). I'd slightly favor compiled over non-compiled, but that's not a major issue.

I found several systems that seem suitable (and I'm a bit familiar with some), but to get objective opinions, I'm interested in what other people would recommend.

Also if you know specific systems that you would highly recommend NOT to use, by all means do share with us. You may save others the same misery :)

Re: Template engines: which one do you recommend?

Posted: Wed Nov 26, 2008 4:55 am
by Eran
Personally, I'm much more for include type templating systems as opposed to search-and-replace systems (such as smarty). Such systems include Savant and Zend_View (which I use for most of my projects). I actually wrote a post about the different templating systems recently (though I didn't go into much detail over specific packages), it might be of interest to you - OO PHP templating (@techfounder).

Re: Template engines: which one do you recommend?

Posted: Wed Nov 26, 2008 7:06 am
by alex.barylski
There is actuallly quite a lot to templating but...

Compiled templating languages essentially go back to the basics and follow what bTemplate started in terms of template systems...

http://massassi.com/php/articles/template_engines/

Smarty when it "compiles" actually just converts it's funny syntax into PHP alternative syntax so "unless" you need the ability to allow end-users to modify templates, there is little point in using Smarty or other, you might as well use your own rolled solution modeled after bTemplate.

In this case, it's dangerous to allow end-users to modify their templates as they contain (can contain) native PHP code such as MySQL, file system, etc...whereas Smarty doesn't have equivelants for many of those functions, so it's compiler would fail if a user attempted to use those, adding a blanket of security to your template system.

Many CMS allow their admins (WordPress) to directly change the templates, including the PHP used to build content areas dynamically.

Cheers,
Alex

Re: Template engines: which one do you recommend?

Posted: Wed Nov 26, 2008 11:28 am
by Apollo
Thanks for your comments. For me, Smarty's alternative syntax wouldn't offer much benefit over regular PHP. And end-users won't be modifying the content anyway, so the security risk of having full PHP access in template pages it not an issue.

I'm starting to wonder whether a template system is really what I need. I thought it would be a nice trade-off between frameworks (which I fear to be just not flexible enough, or too complex, for what I need), and just using straight PHP with a solid library of functions.

An include type templating system based on PHP itself as templating language (rather than inventing a new syntax) may be nice though.

Any other recommendations?

Re: Template engines: which one do you recommend?

Posted: Wed Nov 26, 2008 3:32 pm
by alex.barylski
I'm starting to wonder whether a template system is really what I need.
Define "engine". :P

All you really "need" is a template "layer" such as bTemplate...you can hack on a custom caching interface or addon as required and the code executes as about as fast as you can get.

I do believe that static placeholder replacement is faster than PHP so if your templates are simple enough to warrant using static placeholders and speed is a concern, than by all means implement a simple str_replace like follow:

Code: Select all

$buffer = str_replace(array('##TITLE##','##CONTENT##'). array($title, $content), $buffer);
echo $buffer;
However if you need real control over hos things are rendered then a PHP alternative syntax template layer like bTemplate is all you need.

Smarty and similar template engines don't offer much if any advantage over generic templates, other than the security issue I pointed out earlier, as well as helpers, such as date formatting, etc.

Cheers,
Alex

Re: Template engines: which one do you recommend?

Posted: Thu Nov 27, 2008 3:12 am
by Maugrim_The_Reaper
I just use Zend_View (or something similar) to be honest. All the templates I write are editable by both developers and designers and the whole PHP risk idea is mitigated by review. Smarty is something I'd consider if the general public or a designer I have limited history with is involved in editing.

Re: Template engines: which one do you recommend?

Posted: Fri Nov 28, 2008 8:42 pm
by josh
Are you looking for a template architecture or a templating language. I would use PHP for a template language, if there are security concerns than smarty. For architectural template systems I would use MVC. Zend Framework is 'just' one option.

Re: Template engines: which one do you recommend?

Posted: Fri Nov 28, 2008 11:10 pm
by John Cartwright
jshpro2 wrote:Are you looking for a template architecture or a templating language. I would use PHP for a template language, if there are security concerns than smarty. For architectural template systems I would use MVC. Zend Framework is 'just' one option.
Me too :)

Re: Template engines: which one do you recommend?

Posted: Sat Nov 29, 2008 5:46 am
by Apollo
jshpro2 wrote:For architectural template systems I would use MVC.
With MVC you mean "the Model-View-Controller idea" in general (i.e. something abstract), rather than some explicit implementation or a specific version, right?

Re: Template engines: which one do you recommend?

Posted: Sat Nov 29, 2008 6:37 am
by Kieran Huggins
I love HAML in Ruby, and I've heard of at least one (if not more) PHP ports. I hate writing xhtml now.

http://haml.hamptoncatlin.com/