Skip to content

Commit

Permalink
Merge branch 'release/0.5.5'
Browse files Browse the repository at this point in the history
  • Loading branch information
dominicbarnes committed Jun 12, 2015
2 parents c66c955 + 7ea9d34 commit 1a2ae33
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 2 deletions.
3 changes: 3 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@

# 0.5.5 / 6-12-2015
* allowing front-matter data to be used in layout

# 0.5.4 / 6-9-2015
* Added ability to set local layout to null to render view w/out layout (when defaultLayout is set).

Expand Down
6 changes: 5 additions & 1 deletion lib/renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ Renderer.prototype.compileTemplate = function *(file) {
debug("compiling template %s", chalk.grey(path.relative(this.options.root, file)));
meta.fn = this.handlebars.compile(meta.body);
meta.render = function (locals, options) {
return this.fn(extend(true, {}, locals, this.attributes), options);
return this.fn(locals, options);
};
return meta;
};
Expand Down Expand Up @@ -300,6 +300,10 @@ Renderer.prototype.render = function *(template, locals, options) {
// retrieve view
if (!body) var view = yield this.getView(template);

// extend locals with front-matter data
if (layout && layout.attributes) extend(locals, layout.attributes);
if (view && view.attributes) extend(locals, view.attributes);

// set up some special meta locals before rendering
options.data.view = template;
if (layoutId) options.data.layout = layoutId;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "koa-handlebars",
"version": "0.5.4",
"version": "0.5.5",
"repository": {
"type": "git",
"url": "git@github.com:dominicbarnes/koa-handlebars.git"
Expand Down
5 changes: 5 additions & 0 deletions test/fixtures/layouts/front-matter-data.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
name: Test
---

Layout, {{name}}!
1 change: 1 addition & 0 deletions test/fixtures/layouts/front-matter.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Layout, {{name}}!
21 changes: 21 additions & 0 deletions test/renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,27 @@ describe("Renderer#render(template, locals, options)", function () {

assert.equal(body.trim(), "Hello, World!");
});

it("should ensure that YAML data is accessible to all templates", function *() {
var r = new Renderer({ root: fixture() });
var body = yield r.render("front-matter", { layout: "front-matter" });

assert.equal(body.trim(), "Layout, World!"); // no {{{@body}}}
});

it("should allow YAML in layout", function *() {
var r = new Renderer({ root: fixture() });
var body = yield r.render("simple", { layout: "front-matter-data" });

assert.equal(body.trim(), "Layout, Test!"); // no {{{@body}}}
});

it("should allow layout YAML to be overridden by view YAML", function *() {
var r = new Renderer({ root: fixture() });
var body = yield r.render("front-matter", { layout: "front-matter-data" });

assert.equal(body.trim(), "Layout, World!"); // no {{{@body}}}
});
});

describe("Renderer#middleware()", function () {
Expand Down

0 comments on commit 1a2ae33

Please sign in to comment.