diff --git a/README.md b/README.md index e5ee60a..a969f16 100644 --- a/README.md +++ b/README.md @@ -37,7 +37,16 @@ Options for `#express3` and `#express4` i18n: "{Object} i18n object", layoutsDir: "{String} Path to layout templates", templateOptions: "{Object} options to pass to template()", - beautify: "{Boolean} whether to pretty print HTML, see github.com/einars/js-beautify .jsbeautifyrc + beautify: "{Boolean} whether to pretty print HTML, see github.com/einars/js-beautify .jsbeautifyrc, + + // override the default compile + onCompile: function(exhbs, source, filename) { + var options; + if (filename && filename.indexOf('partials') > -1) { + options = {preventIndent: true}; + } + return exhbs.handlebars.compile(source, options); + } }); diff --git a/lib/hbs.js b/lib/hbs.js index b5514f2..567990d 100644 --- a/lib/hbs.js +++ b/lib/hbs.js @@ -180,7 +180,7 @@ ExpressHbs.prototype.cachePartials = function(cb) { dirname = dirname === '.' ? '' : dirname + '/'; var name = dirname + path.basename(entry.name, path.extname(entry.name)); - self.registerPartial(name, source); + self.registerPartial(name, source, entry.fullPath); }) .on('end', function() { count += 1; @@ -209,7 +209,10 @@ ExpressHbs.prototype.cachePartials = function(cb) { * extname: "extension to use", * contentHelperName: "contentFor", * blockHelperName: "block", - * beautify: "{Boolean} whether to pretty print HTML" + * beautify: "{Boolean} whether to pretty print HTML", + * onCompile: function(self, source, filename) { + * return self.handlebars.compile(source); + * } * } */ ExpressHbs.prototype.express3 = function(options) { @@ -222,6 +225,7 @@ ExpressHbs.prototype.express3 = function(options) { if (!options.blockHelperName) options.blockHelperName = 'block'; if (!options.templateOptions) options.templateOptions = {}; if (options.handlebars) this.handlebars = options.handlebars; + if (options.onCompile) this.onCompile = options.onCompile; this._options = options; if (this._options.handlebars) this.handlebars = this._options.handlebars; @@ -334,8 +338,8 @@ ExpressHbs.prototype.registerHelper = function(name, fn) { * @param {String} name The name of the partial as used in a template. * @param {String} source String source of the partial. */ -ExpressHbs.prototype.registerPartial = function(name, source) { - this.handlebars.registerPartial(name, this.compile(source)); +ExpressHbs.prototype.registerPartial = function(name, source, filename) { + this.handlebars.registerPartial(name, this.compile(source, filename)); }; /** @@ -353,7 +357,14 @@ ExpressHbs.prototype.compile = function(source, filename) { if (source.indexOf('}}') === source.length - 2) { source += ' '; } - var compiled = this.handlebars.compile(source); + + var compiled; + if (this.onCompile) { + compiled = this.onCompile(this, source, filename); + } else { + compiled = this.handlebars.compile(source); + } + if (filename) { if (Array.isArray(this.viewsDir) && this.viewsDir.length > 0) { compiled.__filename = path.relative(this.cwd, filename).replace(path.sep, '/'); diff --git a/package.json b/package.json index 01de8e6..8615be9 100644 --- a/package.json +++ b/package.json @@ -35,7 +35,7 @@ }, "dependencies": { "handlebars": "^3.0.0", - "js-beautify": "~1.5.4", + "js-beautify": "1.5.4", "readdirp": "~1.3.0" } } diff --git a/test/issues.js b/test/issues.js index cbec09b..7420326 100644 --- a/test/issues.js +++ b/test/issues.js @@ -1,3 +1,4 @@ +'use strict'; var assert = require('assert'); var hbs = require('..'); var path = require('path'); @@ -5,7 +6,7 @@ var H = require('./helpers'); describe('issue-22 template', function() { - var dirname = path.join(__dirname, 'issues/22'); + var dirname = path.join(__dirname, 'issues/22'); it('should use multiple layouts with caching', function(done) { var render = hbs.create().express3({}); @@ -125,23 +126,23 @@ describe('issue-21', function() { it('should allow specifying layouts without the parent dir in a sub view', function(done) { function check(err, html) { - assert.ifError(err); - assert.equal('sub', H.stripWs(html)); - done(); - } + assert.ifError(err); + assert.equal('sub', H.stripWs(html)); + done(); + } - var options = {cache: true, layout: 'default', settings: {views: dirname + '/views'}}; - var result = render(dirname + '/views/sub/sub.hbs', options, check); + var options = {cache: true, layout: 'default', settings: {views: dirname + '/views'}}; + var result = render(dirname + '/views/sub/sub.hbs', options, check); }); it('should treat layouts that start with "." relative to template', function(done) { function check(err, html) { - assert.ifError(err); - assert.equal('sub', H.stripWs(html)); - done(); - } + assert.ifError(err); + assert.equal('sub', H.stripWs(html)); + done(); + } - var options = {cache: true, layout: './relativeLayout', settings: {views: dirname + '/views'}}; - var result = render(dirname + '/views/sub/sub.hbs', options, check); + var options = {cache: true, layout: './relativeLayout', settings: {views: dirname + '/views'}}; + var result = render(dirname + '/views/sub/sub.hbs', options, check); }); it('should allow layouts in subfolders', function(done) { @@ -240,45 +241,73 @@ describe('issue-53', function() { }); describe('issue-59', function() { - var dirname = __dirname + '/issues/59'; - it('should escape or not', function (done) { - var hb = hbs.create(); + var dirname = __dirname + '/issues/59'; + it('should escape or not', function (done) { + var hb = hbs.create(); - function async(s, cb) { - cb('' + s + ''); - } + function async(s, cb) { + cb('' + s + ''); + } - hb.registerAsyncHelper("async", async); + hb.registerAsyncHelper("async", async); - var render = hb.express3({ - viewsDir: dirname - }); - var locals = H.createLocals('express3', dirname); + var render = hb.express3({ + viewsDir: dirname + }); + var locals = H.createLocals('express3', dirname); - render(dirname + '/index.hbs', locals, function (err, html) { - assert.equal(H.stripWs(html), '<strong>foo</strong>foo'); - done(); - }); + render(dirname + '/index.hbs', locals, function (err, html) { + assert.equal(H.stripWs(html), '<strong>foo</strong>foo'); + done(); }); - it('should not escape SafeString', function (done) { - var hb = hbs.create(); + }); + it('should not escape SafeString', function (done) { + var hb = hbs.create(); - function async(s, cb) { - cb(new hb.SafeString('' + s + '')); - } + function async(s, cb) { + cb(new hb.SafeString('' + s + '')); + } + + hb.registerAsyncHelper('async', async); + + var render = hb.express3({ + viewsDir: dirname + }); + var locals = H.createLocals('express3', dirname); + + render(dirname + '/index.hbs', locals, function (err, html) { + assert.equal(H.stripWs(html), 'foofoo'); + done(); + }); + }); +}); - hb.registerAsyncHelper("async", async); +describe('issue-73', function() { + var dirname = path.join(__dirname, 'issues/73'); + it('should allow compile options', function(done){ + var hb = hbs.create(); + var render = hb.express3({ + viewsDir: dirname, + partialsDir: dirname + '/partials', + onCompile: function(eh, source, filename) { + var options; + if (filename && filename.indexOf('partials')) { + options = {preventIndent: true}; + } + return eh.handlebars.compile(source, options); + } + }); - var render = hb.express3({ - viewsDir: dirname - }); - var locals = H.createLocals('express3', dirname); + var locals = H.createLocals('express3', dirname); + render(dirname + '/index.hbs', locals, function (err, html) { + if (err) return console.log('error', err); - render(dirname + '/index.hbs', locals, function (err, html) { - assert.equal(H.stripWs(html), 'foofoo'); - done(); - }); + assert.ifError(err); + assert.ok(html.match(/^Hello/m)); + assert.ok(html.match(/^second line/m)); + done(); }); + }); }); diff --git a/test/issues/73/index.hbs b/test/issues/73/index.hbs new file mode 100644 index 0000000..ddfbfeb --- /dev/null +++ b/test/issues/73/index.hbs @@ -0,0 +1,5 @@ +
+
+ {{> partial}} +
+
diff --git a/test/issues/73/partials/partial.html b/test/issues/73/partials/partial.html new file mode 100644 index 0000000..882c413 --- /dev/null +++ b/test/issues/73/partials/partial.html @@ -0,0 +1,6 @@ +
+  
+Hello World, I don't want any indentation
+second line
+  
+