Blog RSS Feed Subscribe

Jordi Boggiano

Jordi Boggiano Passionate web developer, specialized in web performance and php. Partner at Nelmio, information junkie and speaker.

Categories

Who let the Dwoo out ?

Four months have passed since I started on this project and I finally feel that it is stable enough to make an announcement and have more people trying it.

So what is it ? Dwoo is a PHP5 template engine. Another one you might think, indeed but with every new project comes a new vision, and hopefully you will like mine.

If you you don't like chatter, feel free to skip to download.

History

Early this year I wanted to rebuild my template engine to have something stronger to work with, so I started thinking about it and then asked myself why I didn't use one of the available engines out there. The fact is that -and I guess I will lose many of my few readers here- I am not fond of using php itself as a template engine (with Savant or similar).

The two main reasons are that the syntax is too heavy for short variable insertions and that I want to allow end users to edit templates themselves on a site I'm working on. Obviously giving them access to the php files is not an option, so I was left with Smarty-like engines. Most of them are slow, too old, not maintained anymore or just plain ugly, so I didn't like that either.

Smarty being one of the most popular engines, I decided to look at its features to build up my road map, the point being that I knew from the start where I was headed, which has allowed me to avoid many of the weirdnesses that you can find within Smarty. Those being due to backwards compatibility issues or simply the fact that it has been built layer upon layer during many years.

Hence Dwoo was born, built to support (most of) Smarty features at first, but then extended to provide new ways to build templates more efficiently. I also thought that since it followed Smarty features so closely, I should make an easy upgrade path from Smarty to Dwoo. This is possible with the help of an adapter class that translates the Smarty API calls, effectively cloaking Dwoo as a Smarty object.

That means it takes seconds to switch to Dwoo if your app runs Smarty on PHP5 (and well, if you don't use one of the few unsupported features), and then you can make the move smoothly.

Features

I will try to sum up the main features added on top of the Smarty stuff (vars replacement, loops, conditions, caching), feel free to look at the wiki for more detailed examples. Although documentation is a bit scarce at the moment, I will now focus on filling the wiki.

Dwoo is a scope aware engine, some plugins are provided that allow you to change the current scope and there are special variables that read the current, parent or top level (global) scope. One of those plugins is the loop plugin, that iterates over an array and moves the scope within each value, I won't lie it is a tad slower than a pure foreach loop, but if you compare PHP's <?php echo $item['name']; ?> to simply {$name}, it makes the template much shorter and easier to read, so it is probably worth the performance loss unless you are under a lot of traffic - but in this case you might want to use caching anyway.

Template inheritance, a concept that I discovered while looking at Django -a Python framework-, had not yet been ported into any PHP template engine as far as I know. It is basically a new way to build complex template structures by having templates inherit parent templates instead of parent templates including child templates.

Plugins/functions can be called with named arguments as you do in Smarty, but they can also be called without argument names, which is really convenient and easier in most cases.

About plugins, my main issue with Smarty was the use of a $params array that received all parameters as an associative array. The way I solved this is that you just create your plugin with all the parameters you want, with or without default values, and then Dwoo's compiler uses reflection to reorder the parameters in the correct place, checks that arguments with no default value are set in the template, etc. This saves a lot of if(isset($params['bleh'])) junk in the plugins, and there is still the possibility to use a parameter array if a plugin requires it.

Now if you're still with me I hope you will consider trying it, just download it and get started.

May 13, 2008 // PHP

Post a comment:


Formatting: you may use [code php] [/code] (or other languages) for code blocks, links are automatically linked. <strong>, <em> and <blockquote> html tags are allowed without nesting, the rest will be escaped.

Subscribe to this RSS Feed Comments

2008-05-13 05:48:35

Julian

Cool idea, I love the Smarty syntax but it is getting a bit long in the tooth and has a few weird dark corners. Will give Dwoo a try soon.

2008-05-13 10:42:35

Akivinz

That seems a great job man !
I hope for you and Dwoo a big success strory...

See you.

2008-05-13 14:18:27

Mike

Nice work. Some really sweet features and decent test coverage too.

Only thing I'd need before commiting to adopting is some more detailed info on the scope awareness functionality to make sure it wasn't going to bite me. 

 

2008-05-13 15:16:14

DrSlump

Template 'inheritance' and 'context switch' has been around for ages in PHPTAL which is a port of Zope's TAL specification.

IMHO Smarty's is not only bad becaus it's bloated but also because the syntax is a 1:1 match with PHP that there is very little gain in learning and debugging yet another scripting syntax.

Anyway, best of lucks with the project.

2008-05-13 15:50:19

Seldaek

Akivinz, Julian : thanks.

DrSlump : I wouldn't agree entirely with you but it's mostly a matter of opinion and personal preference so I don't think there is much point in arguing about that. And thanks for pointing PHPTAL's inheritance, I wasn't aware of that. Just something that I would like to point out however, is that Smarty's so-called bloatedness is really far from being true. It might not be lightning fast but it is still faster than most of the other engines.

Mike : Don't hesitate to drop by on irc/freenode if you want to talk about it, but honestly it is not something dangerous, besides if you don't use the two plugins I mentionned it doesn't affect you at all.

2008-05-13 23:19:11

gggeek

smarty like syntax and free mixing of positional and named parameters? sounds a lot like the eZ Publish template language/engine... maybe you could hover to the forum and ask developers there what they think about that syntax...

2008-05-16 08:44:50

John Wards

Why why why an other templating language?

 You want to remove all php code from your templates thats cool but how about using a mature well supported pure templating language like XSLT.

 Gives you huge amount of scope, produces almost valid html out of the box and your php code is just spitting out xml which you build progmatically. Zero html in your code and Zero code in your html.

Its fast and stable and mature.

Also its a rather handy skill to pick up too... 

2008-05-16 13:15:27

Seldaek

Well sir, I wouldn't say XSLT doesn't have its merits, it sure is nice, but at the same time it's XML, which means it's extremely verbose, and I don't like that so much.

Besides you have to admit it's more complex to learn XSLT, XML and XPath. I wouldn't mind using XSLT so much personally, but it's harder unless you really know it well, and given the project I am working on, I would not want to have the average user/amateur designer learn all of this. While working in a simple html template with some loop tags etc, is imo much easier.

2008-05-16 14:24:17

John Wards

It is not more complex to learn than another templating language.

The benifts is that is a standard, its a skill you can use in probably every language on the planet.

The company I work for use it, I'd never used it before I started here. Took me one project to pick it up and a online reference guide.

It also means that your code is ready to provide an API for Ajax applications out of the box by just dumping the XML with no styles.

If a designer designs clean XHTML with correct seperation between style and html it makes moving those templates to XSLT even simpler.

Gettting your proccesses correct from the begining by using defined standards and XHTML - XSLT - XML just makes sense. Saves having to code your way out of a mess :) 

2008-05-16 15:07:12

Seldaek

Heh, honestly when knowing PHP, I think the Smarty syntax is really easier to get than learning XSLT which doesn't look anything like it.

However I agree with you that XSLT being a standard is a good thing, and well the fact it's based on XML is also quite useful. My templates are all XHTML, and they are componentized enough that I don't foresee any problem throwing a bit of a page as an AJAX response.

Anyway what I meant was more that both tools have their use, and while XSLT is good, I don't think it fits everywhere.

2008-05-18 19:28:09

Riccardo

Great job. I use Xtemplate and am pretty happy with it, but I'll give Dwoo a try sooner or later.