barc/express-hbs — pull request #35 by olance (2013-11-17 02:55:17+00:00)
Title: Support for nested layouts

Hi!

I found myself in need for nested layouts, to make my life easier, so I had a little fun implementing it into `express-hbs`!

I spent some time studying your code to get how it works and get the thing done with minimal changes. Here's a summary of what I've done:
* The "entry point" for layout inclusion doesn't change: it's either declared in the template given to `compileFile`, given as an option to `render` or as a global `defaultTemplate` option
* However, `cacheLayout` will now check whether the loaded layout template declares a layout (using `{{!< LAYOUT}}` as in templates). If it does, this "parent" layout will be loaded and again, checked for any declared parent layout. The layout hierarchy is loaded up to the top recursively, until no layout declaration is found. This recursion will result in a layout stack listing all layouts templates, from the outermost to the innermost, required to render the page template.
* For each loaded layout, the parents layout stack is cached, just like it was when the code managed a single layout
* Consequently, `render` has been adapted to work with a template and its layouts stack, rendering each layout in the stack with the "concatenated" rendering of previous template/layouts.

The result is that you can just have a `{{!< LAYOUT}}` tag inside any other layout, and the complete layout stack will be rendered & cached correctly.

I also included (simple) tests and expanded the examples to illustrate the feature.

Hope you'll like it!