Skip to content

Commit 147aac1

Browse files
author
Mario L Gutierrez
committed
fix #73
1 parent 36ae3ba commit 147aac1

File tree

6 files changed

+109
-49
lines changed

6 files changed

+109
-49
lines changed

README.md

+10-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,16 @@ Options for `#express3` and `#express4`
3737
i18n: "{Object} i18n object",
3838
layoutsDir: "{String} Path to layout templates",
3939
templateOptions: "{Object} options to pass to template()",
40-
beautify: "{Boolean} whether to pretty print HTML, see github.com/einars/js-beautify .jsbeautifyrc
40+
beautify: "{Boolean} whether to pretty print HTML, see github.com/einars/js-beautify .jsbeautifyrc,
41+
42+
// override the default compile
43+
onCompile: function(exhbs, source, filename) {
44+
var options;
45+
if (filename && filename.indexOf('partials') > -1) {
46+
options = {preventIndent: true};
47+
}
48+
return exhbs.handlebars.compile(source, options);
49+
}
4150
});
4251

4352

lib/hbs.js

+16-5
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ ExpressHbs.prototype.cachePartials = function(cb) {
180180
dirname = dirname === '.' ? '' : dirname + '/';
181181

182182
var name = dirname + path.basename(entry.name, path.extname(entry.name));
183-
self.registerPartial(name, source);
183+
self.registerPartial(name, source, entry.fullPath);
184184
})
185185
.on('end', function() {
186186
count += 1;
@@ -209,7 +209,10 @@ ExpressHbs.prototype.cachePartials = function(cb) {
209209
* extname: "extension to use",
210210
* contentHelperName: "contentFor",
211211
* blockHelperName: "block",
212-
* beautify: "{Boolean} whether to pretty print HTML"
212+
* beautify: "{Boolean} whether to pretty print HTML",
213+
* onCompile: function(self, source, filename) {
214+
* return self.handlebars.compile(source);
215+
* }
213216
* }
214217
*/
215218
ExpressHbs.prototype.express3 = function(options) {
@@ -222,6 +225,7 @@ ExpressHbs.prototype.express3 = function(options) {
222225
if (!options.blockHelperName) options.blockHelperName = 'block';
223226
if (!options.templateOptions) options.templateOptions = {};
224227
if (options.handlebars) this.handlebars = options.handlebars;
228+
if (options.onCompile) this.onCompile = options.onCompile;
225229

226230
this._options = options;
227231
if (this._options.handlebars) this.handlebars = this._options.handlebars;
@@ -334,8 +338,8 @@ ExpressHbs.prototype.registerHelper = function(name, fn) {
334338
* @param {String} name The name of the partial as used in a template.
335339
* @param {String} source String source of the partial.
336340
*/
337-
ExpressHbs.prototype.registerPartial = function(name, source) {
338-
this.handlebars.registerPartial(name, this.compile(source));
341+
ExpressHbs.prototype.registerPartial = function(name, source, filename) {
342+
this.handlebars.registerPartial(name, this.compile(source, filename));
339343
};
340344

341345
/**
@@ -353,7 +357,14 @@ ExpressHbs.prototype.compile = function(source, filename) {
353357
if (source.indexOf('}}') === source.length - 2) {
354358
source += ' ';
355359
}
356-
var compiled = this.handlebars.compile(source);
360+
361+
var compiled;
362+
if (this.onCompile) {
363+
compiled = this.onCompile(this, source, filename);
364+
} else {
365+
compiled = this.handlebars.compile(source);
366+
}
367+
357368
if (filename) {
358369
if (Array.isArray(this.viewsDir) && this.viewsDir.length > 0) {
359370
compiled.__filename = path.relative(this.cwd, filename).replace(path.sep, '/');

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
},
3636
"dependencies": {
3737
"handlebars": "^3.0.0",
38-
"js-beautify": "~1.5.4",
38+
"js-beautify": "1.5.4",
3939
"readdirp": "~1.3.0"
4040
}
4141
}

test/issues.js

+71-42
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1+
'use strict';
12
var assert = require('assert');
23
var hbs = require('..');
34
var path = require('path');
45
var H = require('./helpers');
56

67

78
describe('issue-22 template', function() {
8-
var dirname = path.join(__dirname, 'issues/22');
9+
var dirname = path.join(__dirname, 'issues/22');
910

1011
it('should use multiple layouts with caching', function(done) {
1112
var render = hbs.create().express3({});
@@ -125,23 +126,23 @@ describe('issue-21', function() {
125126

126127

127128
it('should allow specifying layouts without the parent dir in a sub view', function(done) { function check(err, html) {
128-
assert.ifError(err);
129-
assert.equal('<html>sub</html>', H.stripWs(html));
130-
done();
131-
}
129+
assert.ifError(err);
130+
assert.equal('<html>sub</html>', H.stripWs(html));
131+
done();
132+
}
132133

133-
var options = {cache: true, layout: 'default', settings: {views: dirname + '/views'}};
134-
var result = render(dirname + '/views/sub/sub.hbs', options, check);
134+
var options = {cache: true, layout: 'default', settings: {views: dirname + '/views'}};
135+
var result = render(dirname + '/views/sub/sub.hbs', options, check);
135136
});
136137

137138
it('should treat layouts that start with "." relative to template', function(done) { function check(err, html) {
138-
assert.ifError(err);
139-
assert.equal('<relative>sub</relative>', H.stripWs(html));
140-
done();
141-
}
139+
assert.ifError(err);
140+
assert.equal('<relative>sub</relative>', H.stripWs(html));
141+
done();
142+
}
142143

143-
var options = {cache: true, layout: './relativeLayout', settings: {views: dirname + '/views'}};
144-
var result = render(dirname + '/views/sub/sub.hbs', options, check);
144+
var options = {cache: true, layout: './relativeLayout', settings: {views: dirname + '/views'}};
145+
var result = render(dirname + '/views/sub/sub.hbs', options, check);
145146
});
146147

147148
it('should allow layouts in subfolders', function(done) {
@@ -240,45 +241,73 @@ describe('issue-53', function() {
240241
});
241242

242243
describe('issue-59', function() {
243-
var dirname = __dirname + '/issues/59';
244-
it('should escape or not', function (done) {
245-
var hb = hbs.create();
244+
var dirname = __dirname + '/issues/59';
245+
it('should escape or not', function (done) {
246+
var hb = hbs.create();
246247

247-
function async(s, cb) {
248-
cb('<strong>' + s + '</strong>');
249-
}
248+
function async(s, cb) {
249+
cb('<strong>' + s + '</strong>');
250+
}
250251

251-
hb.registerAsyncHelper("async", async);
252+
hb.registerAsyncHelper("async", async);
252253

253-
var render = hb.express3({
254-
viewsDir: dirname
255-
});
256-
var locals = H.createLocals('express3', dirname);
254+
var render = hb.express3({
255+
viewsDir: dirname
256+
});
257+
var locals = H.createLocals('express3', dirname);
257258

258-
render(dirname + '/index.hbs', locals, function (err, html) {
259-
assert.equal(H.stripWs(html), '&lt;strong&gt;foo&lt;/strong&gt;<strong>foo</strong>');
260-
done();
261-
});
259+
render(dirname + '/index.hbs', locals, function (err, html) {
260+
assert.equal(H.stripWs(html), '&lt;strong&gt;foo&lt;/strong&gt;<strong>foo</strong>');
261+
done();
262262
});
263-
it('should not escape SafeString', function (done) {
264-
var hb = hbs.create();
263+
});
264+
it('should not escape SafeString', function (done) {
265+
var hb = hbs.create();
265266

266-
function async(s, cb) {
267-
cb(new hb.SafeString('<em>' + s + '</em>'));
268-
}
267+
function async(s, cb) {
268+
cb(new hb.SafeString('<em>' + s + '</em>'));
269+
}
270+
271+
hb.registerAsyncHelper('async', async);
272+
273+
var render = hb.express3({
274+
viewsDir: dirname
275+
});
276+
var locals = H.createLocals('express3', dirname);
277+
278+
render(dirname + '/index.hbs', locals, function (err, html) {
279+
assert.equal(H.stripWs(html), '<em>foo</em><em>foo</em>');
280+
done();
281+
});
282+
});
283+
});
269284

270-
hb.registerAsyncHelper("async", async);
285+
describe('issue-73', function() {
286+
var dirname = path.join(__dirname, 'issues/73');
287+
it('should allow compile options', function(done){
288+
var hb = hbs.create();
289+
var render = hb.express3({
290+
viewsDir: dirname,
291+
partialsDir: dirname + '/partials',
292+
onCompile: function(eh, source, filename) {
293+
var options;
294+
if (filename && filename.indexOf('partials')) {
295+
options = {preventIndent: true};
296+
}
297+
return eh.handlebars.compile(source, options);
298+
}
299+
});
271300

272-
var render = hb.express3({
273-
viewsDir: dirname
274-
});
275-
var locals = H.createLocals('express3', dirname);
301+
var locals = H.createLocals('express3', dirname);
302+
render(dirname + '/index.hbs', locals, function (err, html) {
303+
if (err) return console.log('error', err);
276304

277-
render(dirname + '/index.hbs', locals, function (err, html) {
278-
assert.equal(H.stripWs(html), '<em>foo</em><em>foo</em>');
279-
done();
280-
});
305+
assert.ifError(err);
306+
assert.ok(html.match(/^Hello/m));
307+
assert.ok(html.match(/^second line/m));
308+
done();
281309
});
310+
});
282311
});
283312

284313

test/issues/73/index.hbs

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<div>
2+
<div>
3+
{{> partial}}
4+
</div>
5+
</div>

test/issues/73/partials/partial.html

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<pre>
2+
<code>
3+
Hello World, I don't want any indentation
4+
second line
5+
</code>
6+
</pre>

0 commit comments

Comments
 (0)