Skip to content

Commit

Permalink
disableLayoutDirective
Browse files Browse the repository at this point in the history
  • Loading branch information
Mario L Gutierrez committed Jan 17, 2014
1 parent 0ecca1c commit 2a9fcfd
Show file tree
Hide file tree
Showing 9 changed files with 133 additions and 55 deletions.
8 changes: 8 additions & 0 deletions .editorconfig
@@ -0,0 +1,8 @@
; indicate this is the root of the project

[*]
indent_style = space
end_of_line = lf

[*.js]
indent_size = 2
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -33,6 +33,7 @@ Options for `#express3`
extname: "{String} Extension for templates, defaults to `.hbs`",
handlebars: "{Module} Use external handlebars instead of express-hbs dependency",
layoutsDir: "{String} Path to layout templates",
disableLayoutDirective: "{Boolean} Disables comment-based layout directives",
templateOptions: "{Object} options to pass to template()"
});

Expand Down
40 changes: 20 additions & 20 deletions gruntfile.js
@@ -1,26 +1,26 @@
module.exports = function configureGrunt(grunt) {
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),

mochacli: {
options: {
ui: 'bdd',
reporter: 'spec'
},
mochacli: {
options: {
ui: 'bdd',
reporter: 'spec'
},

all: {
src: ['test/*.js']
}
}
});
all: {
src: ['test/*.js']
}
}
});

grunt.registerTask('setProductionEnv', function () {
// Use 'production' config
process.env.NODE_ENV = 'production';
});
grunt.registerTask('setProductionEnv', function () {
// Use 'production' config
process.env.NODE_ENV = 'production';
});

grunt.loadNpmTasks('grunt-mocha-cli');
grunt.loadNpmTasks('grunt-mocha-cli');

// Run tests
grunt.registerTask('default', ['mochacli:all', 'setProductionEnv', 'mochacli:all']);
};
// Run tests
grunt.registerTask('default', ['mochacli:all', 'setProductionEnv', 'mochacli:all']);
};
13 changes: 10 additions & 3 deletions lib/hbs.js
Expand Up @@ -76,9 +76,12 @@ ExpressHbs.prototype.layoutPath = function(filename, layout) {
* Find the path of the declared layout in `str`, if any
*
* @param {String} str The template string to parse
* @return {String} filename Path to template
* @param {String} filename Path to template
* @returns {String|undefined} Returns the path to layout.
*/
ExpressHbs.prototype.declaredLayoutFile = function(str, filename) {
if (this.disableLayoutDirective) return;

var matches = str.match(layoutPattern);
if (matches) {
var layout = matches[1];
Expand All @@ -89,7 +92,7 @@ ExpressHbs.prototype.declaredLayoutFile = function(str, filename) {
}
return path.resolve(path.dirname(filename), layout);
}
}
};

/**
* Compiles a layout file.
Expand Down Expand Up @@ -203,7 +206,8 @@ ExpressHbs.prototype.cachePartials = function(cb) {
* layoutsDir: "absolute path to the layouts",
* extname: "extension to use",
* contentHelperName: "contentFor",
* blockHelperName: "block"
* blockHelperName: "block",
* disableLayoutDirective {Boolean}: "disable template defined layouts"
* }
*
*/
Expand Down Expand Up @@ -240,6 +244,8 @@ ExpressHbs.prototype.express3 = function(options) {
// Absolute path to the layouts directory
this.layoutsDir = this._options.layoutsDir;

// Comment based layouts {{!< LAYOUT}}
this.disableLayoutDirective = Boolean(this._options.disableLayoutDirective);

// Cache for templates, express 3.x doesn't do this for us
this.cache = {};
Expand All @@ -254,6 +260,7 @@ ExpressHbs.prototype.express3 = function(options) {
// Keep track of if partials have been cached already or not.
this.isPartialCachingComplete = false;


return _express3.bind(this);
};

Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "express-hbs",
"version": "0.7.0-pre",
"version": "0.7.1-pre",
"description": "Express 3 handlebars template engine complete with multiple layouts, partials and blocks.",
"keywords": [
"express3",
Expand Down
33 changes: 2 additions & 31 deletions test/handlebarsSpecs.js
@@ -1,6 +1,6 @@
var request = require('supertest');
var assert = require('assert');

var hbs = require('..');

describe('express-hbs', function() {

Expand All @@ -9,8 +9,7 @@ describe('express-hbs', function() {

beforeEach(function() {
var example = require('../example/app');
var hbs = require('..');
app = example.create(hbs);
app = example.create(hbs.create());
});


Expand Down Expand Up @@ -85,36 +84,8 @@ describe('express-hbs', function() {

});

describe('layoutsDir', function() {
var app;

beforeEach(function() {
app = require('../example/app-layoutsDir');
});

it('should render layout declared in markup', function(done) {
request(app)
.get('/fruits')
.expect(/DECLARATIVE LAYOUT/, done);
});

it('should allow specifying layout in locals without dir', function(done) {
request(app)
.get('/veggies')
.expect(/PROGRAMMATIC LAYOUT/, done);
});

it('should still allow specifying layout in locals with dir', function(done) {
request(app)
.get('/veggies/explicit-dir')
.expect(/PROGRAMMATIC LAYOUT/, done);
});

});

describe('instances', function() {
it('should create isolated instances', function() {
var hbs = require('..');
var hbs2 = hbs.create();
var hbs3 = hbs.create();

Expand Down
88 changes: 88 additions & 0 deletions test/layoutSpecs.js
@@ -0,0 +1,88 @@
var request = require('supertest');
var assert = require('assert');
var hbs = require('..');


function stripWs(s) {
return s.replace(/\s+/g, '');
}

function createLocals(which, viewsDir, locals) {
if (!locals) locals = {};
var opts = {};
if (which === 'express3') {
opts.settings = {
views: viewsDir,
};
opts.cache = process.env.NODE_ENV === 'production';
opts.settings.views = viewsDir;
for (var k in locals) {
if (!locals.hasOwnProperty(k)) continue;
opts[k] = locals[k];
}
}
return opts;
}

describe('layouts', function() {
var app;

beforeEach(function() {
var example = require('../example/app');
app = example.create(hbs.create());
});


describe('layoutsDir', function() {
var app;

beforeEach(function() {
app = require('../example/app-layoutsDir');
});

it('should render layout declared in markup', function(done) {
request(app)
.get('/fruits')
.expect(/DECLARATIVE LAYOUT/, done);
});

it('should allow specifying layout in locals without dir', function(done) {
request(app)
.get('/veggies')
.expect(/PROGRAMMATIC LAYOUT/, done);
});

it('should still allow specifying layout in locals with dir', function(done) {
request(app)
.get('/veggies/explicit-dir')
.expect(/PROGRAMMATIC LAYOUT/, done);
});
});

describe('disableLayoutDirective', function() {
var dirname = __dirname + '/views/disableLayoutDirective';

it ('should process directive without option', function(done) {
var render = hbs.create().express3({
});
var locals = createLocals('express3', dirname);

render(dirname + '/index.hbs', locals, function(err, html) {
assert.equal('<dld>dld</dld>', stripWs(html));
done();
});
});

it ('should not process directive with option set', function(done) {
var render = hbs.create().express3({
disableLayoutDirective: true
});
var locals = createLocals('express3', dirname);
render(dirname + '/index.hbs', locals, function(err, html) {
assert.equal('dld', stripWs(html));
done();
});
});
});

});
2 changes: 2 additions & 0 deletions test/views/disableLayoutDirective/index.hbs
@@ -0,0 +1,2 @@
{{!< layouts/default}}
dld
1 change: 1 addition & 0 deletions test/views/disableLayoutDirective/layouts/default.hbs
@@ -0,0 +1 @@
<dld>{{{body}}}</dld>

0 comments on commit 2a9fcfd

Please sign in to comment.