Template engines: which one do you recommend?

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
User avatar
Apollo
Forum Regular
Posts: 794
Joined: Wed Apr 30, 2008 2:34 am

Template engines: which one do you recommend?

Post 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 :)
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Template engines: which one do you recommend?

Post 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).
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: Template engines: which one do you recommend?

Post 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
User avatar
Apollo
Forum Regular
Posts: 794
Joined: Wed Apr 30, 2008 2:34 am

Re: Template engines: which one do you recommend?

Post 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?
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: Template engines: which one do you recommend?

Post 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
User avatar
Maugrim_The_Reaper
DevNet Master
Posts: 2704
Joined: Tue Nov 02, 2004 5:43 am
Location: Ireland

Re: Template engines: which one do you recommend?

Post 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.
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: Template engines: which one do you recommend?

Post 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.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Template engines: which one do you recommend?

Post 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 :)
User avatar
Apollo
Forum Regular
Posts: 794
Joined: Wed Apr 30, 2008 2:34 am

Re: Template engines: which one do you recommend?

Post 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?
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Re: Template engines: which one do you recommend?

Post 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/
Post Reply