Skip to content

Commit

Permalink
Fix an issue where @DaTa does not work in layouts
Browse files Browse the repository at this point in the history
  • Loading branch information
gnowoel committed Jun 10, 2015
1 parent aa608f0 commit 2eb4431
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 8 deletions.
11 changes: 6 additions & 5 deletions lib/hbs.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ function middleware(filename, options, cb) {
var self = this;
var cache = self.cache;
var handlebars = self.handlebars;

self.async = async();

// grab extension from filename
Expand Down Expand Up @@ -59,7 +59,6 @@ function middleware(filename, options, cb) {

try {
var data = locals.__hbsLocals;
delete locals.__hbsLocals;
var res = template(locals, { data: data });
self.async.done(function(values) {
Object.keys(values).forEach(function(id) {
Expand All @@ -85,7 +84,9 @@ function middleware(filename, options, cb) {
var locals = options;
locals.body = str;

var res = template(locals);
var data = locals.__hbsLocals;
delete locals.__hbsLocals;
var res = template(locals, { data: data });
self.async.done(function(values) {
Object.keys(values).forEach(function(id) {
res = res.replace(id, values[id]);
Expand Down Expand Up @@ -115,7 +116,7 @@ function middleware(filename, options, cb) {

var layout_filename = [].concat(view_dirs).map(function (view_dir) {
var view_path = path.join(view_dir, layout || 'layout');

if (!path.extname(view_path)) {
view_path += extension;
}
Expand Down Expand Up @@ -170,7 +171,7 @@ function middleware(filename, options, cb) {
}

cacheAndCompile(str);
});
});
}

tryReadFileAndCache(layout_filename);
Expand Down
24 changes: 24 additions & 0 deletions test/3.x/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ hbs.registerPartials(__dirname + '/views/partials');

// expose app and response locals in views
hbs.localsAsTemplateData(app);
app.locals.father = 'Alan';

app.get('/', function(req, res){
res.render('index', {
Expand Down Expand Up @@ -139,6 +140,13 @@ app.get('/locals', function(req, res) {
});
});

app.get('/globals', function(req, res) {
res.render('globals', {
layout: 'layout_globals',
kids: [{ name: 'Jimmy' }, { name: 'Sally' }],
});
});

test('index', function(done) {
var server = app.listen(3000, function() {

Expand Down Expand Up @@ -233,3 +241,19 @@ test('response locals', function(done) {
done();
});
});

test('response globals', function(done) {
var server = app.listen(3000, function() {

var expected = fs.readFileSync(__dirname + '/../fixtures/globals.html', 'utf8');

request('http://localhost:3000/globals', function(err, res, body) {
assert.equal(body, expected);
server.close();
});
});

server.on('close', function() {
done();
});
});
3 changes: 3 additions & 0 deletions test/3.x/views/globals.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{{#each kids}}
{{@father}} has a kid named {{name}}.
{{/each}}
3 changes: 3 additions & 0 deletions test/3.x/views/layout_globals.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{{@father}}'s Wonderful Kids

{{{body}}}
30 changes: 27 additions & 3 deletions test/4.x/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ hbs.registerPartials(__dirname + '/views/partials');

// expose app and response locals in views
hbs.localsAsTemplateData(app);
app.locals.father = 'Alan';

app.get('/', function(req, res){
res.render('index', {
Expand Down Expand Up @@ -135,10 +136,17 @@ app.get('/locals', function(req, res) {
});
});

app.get('/globals', function(req, res) {
res.render('globals', {
layout: 'layout_globals',
kids: [{ name: 'Jimmy' }, { name: 'Sally' }],
});
});

app.get('/secondary', function(req, res) {
res.render('secondary', {
text: ' index body :)',
title: 'Express Handlebars Test'
res.render('secondary', {
text: ' index body :)',
title: 'Express Handlebars Test'
});
});

Expand Down Expand Up @@ -242,6 +250,22 @@ test('response locals', function(done) {
});
});

test('response globals', function(done) {
var server = app.listen(3000, function() {

var expected = fs.readFileSync(__dirname + '/../fixtures/globals.html', 'utf8');

request('http://localhost:3000/globals', function(err, res, body) {
assert.equal(body, expected);
server.close();
});
});

server.on('close', function() {
done();
});
});

test('multiple views directories', function(done) {
var server = app.listen(3000, function() {

Expand Down
3 changes: 3 additions & 0 deletions test/4.x/views/globals.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{{#each kids}}
{{@father}} has a kid named {{name}}.
{{/each}}
3 changes: 3 additions & 0 deletions test/4.x/views/layout_globals.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{{@father}}'s Wonderful Kids

{{{body}}}
4 changes: 4 additions & 0 deletions test/fixtures/globals.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Alan's Wonderful Kids

Alan has a kid named Jimmy.
Alan has a kid named Sally.

0 comments on commit 2eb4431

Please sign in to comment.