Skip to content

Commit

Permalink
Merge pull request #9 from ivanstojic/master
Browse files Browse the repository at this point in the history
Suppressing default layout from a render call
  • Loading branch information
Mario Gutierrez committed Jan 2, 2013
2 parents 5d7b802 + 83c6852 commit 9708358
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
12 changes: 11 additions & 1 deletion README.md
Expand Up @@ -47,7 +47,7 @@ To define block content in a page.
CONTENT HERE
{{/contentFor}}

There are three ways to use a layout
There are three ways to use a layout, listed in the order in which they are checked for and used:

1. Declarative within a page. Use handlebars comment. `LAYOUT` is a relative path from template.

Expand All @@ -60,6 +60,16 @@ There are three ways to use a layout
veggies: veggies,
layout: 'layout/veggie'
});

This option also allows for default layout suppression by passing in a falsey Javascript value as the value of the `layout` property:

```
res.render('veggies', {
title: 'My favorite veggies',
veggies: veggies,
layout: null // render without using a layout template
});
```

3. Lastly, use `defaultLayout` if specified in hbs configuration options.

Expand Down
18 changes: 12 additions & 6 deletions lib/hbs.js
Expand Up @@ -272,12 +272,18 @@ var _express3 = function(filename, options, cb) {
}

// 2. Layout specified by options from render
else if (options.layout) {
var layoutFile = path.resolve(path.join(path.dirname(filename), options.layout));
cacheLayout(layoutFile, options.cache, function(err, layoutTemplate) {
if (err) return cb(err);
renderIt(layoutTemplate);
});
else if (typeof(options.layout) !== 'undefined') {
if (options.layout) {
var layoutFile = path.resolve(path.join(path.dirname(filename), options.layout));
cacheLayout(layoutFile, options.cache, function(err, layoutTemplate) {
if (err) return cb(err);
renderIt(layoutTemplate);
});

} else {
// if the value is falsey, behave as if no layout should be used - suppress defaults
renderIt(null);
}
}

// 3. Default layout specified when middleware was configured.
Expand Down

0 comments on commit 9708358

Please sign in to comment.