Skip to content

Commit

Permalink
fix #73
Browse files Browse the repository at this point in the history
  • Loading branch information
Mario L Gutierrez committed Mar 12, 2015
1 parent 36ae3ba commit 147aac1
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 49 deletions.
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
});


Expand Down
21 changes: 16 additions & 5 deletions lib/hbs.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand All @@ -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;
Expand Down Expand Up @@ -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));
};

/**
Expand All @@ -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, '/');
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
},
"dependencies": {
"handlebars": "^3.0.0",
"js-beautify": "~1.5.4",
"js-beautify": "1.5.4",
"readdirp": "~1.3.0"
}
}
113 changes: 71 additions & 42 deletions test/issues.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
'use strict';
var assert = require('assert');
var hbs = require('..');
var path = require('path');
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({});
Expand Down Expand Up @@ -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('<html>sub</html>', H.stripWs(html));
done();
}
assert.ifError(err);
assert.equal('<html>sub</html>', 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('<relative>sub</relative>', H.stripWs(html));
done();
}
assert.ifError(err);
assert.equal('<relative>sub</relative>', 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) {
Expand Down Expand Up @@ -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('<strong>' + s + '</strong>');
}
function async(s, cb) {
cb('<strong>' + s + '</strong>');
}

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), '&lt;strong&gt;foo&lt;/strong&gt;<strong>foo</strong>');
done();
});
render(dirname + '/index.hbs', locals, function (err, html) {
assert.equal(H.stripWs(html), '&lt;strong&gt;foo&lt;/strong&gt;<strong>foo</strong>');
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('<em>' + s + '</em>'));
}
function async(s, cb) {
cb(new hb.SafeString('<em>' + s + '</em>'));
}

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), '<em>foo</em><em>foo</em>');
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), '<em>foo</em><em>foo</em>');
done();
});
assert.ifError(err);
assert.ok(html.match(/^Hello/m));
assert.ok(html.match(/^second line/m));
done();
});
});
});


Expand Down
5 changes: 5 additions & 0 deletions test/issues/73/index.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<div>
<div>
{{> partial}}
</div>
</div>
6 changes: 6 additions & 0 deletions test/issues/73/partials/partial.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<pre>
<code>
Hello World, I don't want any indentation
second line
</code>
</pre>

0 comments on commit 147aac1

Please sign in to comment.