From a5a42f8dc1600354c209f81aad4520301c062f81 Mon Sep 17 00:00:00 2001 From: Wyatt Preul Date: Tue, 25 Jun 2013 10:29:19 -0500 Subject: [PATCH 1/3] Layouts work correctly in jade --- lib/views.js | 5 +--- test/unit/templates/valid/index.jade | 4 +++ test/unit/templates/valid/test.jade | 1 + test/unit/views.js | 37 +++++++++++++++++++++++++--- 4 files changed, 39 insertions(+), 8 deletions(-) create mode 100644 test/unit/templates/valid/index.jade create mode 100644 test/unit/templates/valid/test.jade diff --git a/lib/views.js b/lib/views.js index c45ff769e..460ded5aa 100755 --- a/lib/views.js +++ b/lib/views.js @@ -218,11 +218,8 @@ internals.Manager.prototype.render = function (filename, context, options, callb return callback(err); } - var layoutContext = Utils.clone(context); - try { - layoutContext[settings.layoutKeyword] = compiled(context, settings.runtimeOptions); - rendered = layout(layoutContext, settings.runtimeOptions); + rendered = compiled(context, settings.runtimeOptions); } catch (err) { return callback(Boom.internal(err.message, err)); diff --git a/test/unit/templates/valid/index.jade b/test/unit/templates/valid/index.jade new file mode 100644 index 000000000..37fa7f08d --- /dev/null +++ b/test/unit/templates/valid/index.jade @@ -0,0 +1,4 @@ +extends layout + +block content + p= message \ No newline at end of file diff --git a/test/unit/templates/valid/test.jade b/test/unit/templates/valid/test.jade new file mode 100644 index 000000000..e8fee38eb --- /dev/null +++ b/test/unit/templates/valid/test.jade @@ -0,0 +1 @@ +p= message \ No newline at end of file diff --git a/test/unit/views.js b/test/unit/views.js index fa0d472bc..ca5056744 100755 --- a/test/unit/views.js +++ b/test/unit/views.js @@ -37,6 +37,18 @@ describe('Views', function () { layout: true }); + var testViewWithJadeLayouts = new Views({ + engines: { 'jade': 'jade' }, + path: viewsPath + '/valid/', + layout: true + }); + + var testViewWithoutJadeLayouts = new Views({ + engines: { 'jade': 'jade' }, + path: viewsPath + '/valid/', + layout: false + }); + it('renders with async compile', function (done) { var views = new Views({ @@ -57,7 +69,7 @@ describe('Views', function () { views.render('valid/test', { title: 'test', message: 'Hapi' }, null, function (err, rendered, config) { expect(rendered).to.exist; - expect(rendered.length).above(1); + expect(rendered).to.contain('Hapi'); done(); }); }); @@ -92,7 +104,7 @@ describe('Views', function () { testView.render('valid/test', { title: 'test', message: 'Hapi' }, null, function (err, rendered, config) { expect(rendered).to.exist; - expect(rendered.length).above(1); + expect(rendered).to.contain('Hapi'); done(); }); }); @@ -102,10 +114,27 @@ describe('Views', function () { testViewWithLayouts.render('valid/test', { title: 'test', message: 'Hapi' }, null, function (err, rendered, config) { expect(rendered).to.exist; - expect(rendered.length).above(1); + expect(rendered).to.contain('Hapi'); done(); }); + }); + + it('should work and not throw with valid jade layouts', function (done) { + testViewWithJadeLayouts.render('index', { title: 'test', message: 'Hapi' }, null, function (err, rendered, config) { + + expect(rendered).to.contain('Hapi'); + done(); + }); + }); + + it('should work and not throw without jade layouts', function (done) { + + testViewWithoutJadeLayouts.render('test', { title: 'test', message: 'Hapi Message' }, null, function (err, rendered, config) { + + expect(rendered).to.contain('Hapi Message'); + done(); + }); }); it('should work and not throw with basePath, template name, and no path', function (done) { @@ -114,7 +143,7 @@ describe('Views', function () { views.render('test', { title: 'test', message: 'Hapi' }, { basePath: viewsPath + '/valid' }, function (err, rendered, config) { expect(rendered).to.exist; - expect(rendered.length).above(1); + expect(rendered).to.contain('Hapi'); done(); }); }); From e53964372c7e98330e62c2b7805bf5bcad92b95f Mon Sep 17 00:00:00 2001 From: Wyatt Preul Date: Tue, 25 Jun 2013 12:19:02 -0500 Subject: [PATCH 2/3] Reverting view change and fixing test --- lib/views.js | 5 ++++- test/unit/templates/valid/layout.jade | 3 ++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/lib/views.js b/lib/views.js index 460ded5aa..c45ff769e 100755 --- a/lib/views.js +++ b/lib/views.js @@ -218,8 +218,11 @@ internals.Manager.prototype.render = function (filename, context, options, callb return callback(err); } + var layoutContext = Utils.clone(context); + try { - rendered = compiled(context, settings.runtimeOptions); + layoutContext[settings.layoutKeyword] = compiled(context, settings.runtimeOptions); + rendered = layout(layoutContext, settings.runtimeOptions); } catch (err) { return callback(Boom.internal(err.message, err)); diff --git a/test/unit/templates/valid/layout.jade b/test/unit/templates/valid/layout.jade index aa163fabd..658a3e32b 100644 --- a/test/unit/templates/valid/layout.jade +++ b/test/unit/templates/valid/layout.jade @@ -1,3 +1,4 @@ html body - block content \ No newline at end of file + block content + =content \ No newline at end of file From b7849ed32f5eb377b076838bef47bdb78372d6f9 Mon Sep 17 00:00:00 2001 From: Wyatt Preul Date: Tue, 25 Jun 2013 12:27:43 -0500 Subject: [PATCH 3/3] Adding message about layout --- docs/Reference.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/Reference.md b/docs/Reference.md index ad8b1f72a..f27d722ad 100755 --- a/docs/Reference.md +++ b/docs/Reference.md @@ -249,8 +249,8 @@ When creating a server instance, the following options configure the server's be not supported and are ignored. Defaults to no helpers support (empty path). - `basePath` - a base path used as prefix for `path` and `partialsPath`. No default. - `layout` - if set to `true`, layout support is enabled. A layout is a single template file used as the parent template for other view templates - in the same engine. The layout template name must be 'layout.ext' where 'ext' is the engine's extension. - Defaults to `false`. + in the same engine. The layout template name must be 'layout.ext' where 'ext' is the engine's extension. Disable 'layout' when using Jade as + it will handle including any layout files independently of Hapi. Defaults to `false`. - `layoutKeyword` - the key used by the template engine to denote where primary template content should go. Defaults to `'content'`. - `encoding` - the text encoding used by the templates when reading the files and outputting the result. Defaults to `'utf-8'`. - `isCached` - if set to `false`, templates will not be cached (thus will be read from file on every use). Defaults to `true`.