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('
+
+Hello World, I don't want any indentation
+second line
+
+