Skip to content

Commit 03219a2

Browse files
committed
Passing in template name to dust compile.
This will ensure when context.getTemplateName is called in a base template the appropriate name is available
1 parent 894b0de commit 03219a2

File tree

4 files changed

+47
-1
lines changed

4 files changed

+47
-1
lines changed

lib/consolidate.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,12 @@ exports.dust.render = function(str, options, fn){
383383
};
384384

385385
try {
386-
var tmpl = cache(options) || cache(options, engine.compileFn(str));
386+
var templateName;
387+
if (options.filename) {
388+
templateName = options.filename.replace(new RegExp('^' + views + '/'), '').replace(new RegExp('\\.' + ext), '');
389+
}
390+
391+
var tmpl = cache(options) || cache(options, engine.compileFn(str, templateName));
387392
tmpl(options, fn);
388393
} catch (err) {
389394
fn(err);

test/consolidate.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ require('./shared').test('hogan');
1919
require('./shared/partials').test('hogan');
2020
require('./shared').test('dust');
2121
require('./shared/partials').test('dust');
22+
require('./shared/dust').test('dust');
2223
require('./shared').test('handlebars');
2324
require('./shared/partials').test('handlebars');
2425
require('./shared/helpers').test('handlebars');
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<p>{user.name}</p>
2+
{@templateName/}

test/shared/dust.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*eslint-env node, mocha */
2+
var cons = require('../../');
3+
var fs = require('fs');
4+
5+
// var should = require('should');
6+
7+
exports.test = function(name) {
8+
var user = { name: 'Tobi' };
9+
10+
describe(name, function(){
11+
// Use case: return upper case string.
12+
it('should support fetching template name from the context', function(done) {
13+
var viewsDir = 'test/fixtures/' + name;
14+
var templatePath = viewsDir + '/user_template_name.' + name;
15+
var str = fs.readFileSync(templatePath).toString();
16+
17+
var locals = {
18+
user: user,
19+
views: viewsDir,
20+
filename: templatePath
21+
};
22+
23+
if (name === 'dust') {
24+
var dust = require('dustjs-helpers');
25+
dust.helpers.templateName = function(chunk, context, bodies, params) {
26+
return chunk.write(context.getTemplateName());
27+
};
28+
cons.requires.dust = dust;
29+
}
30+
31+
cons[name].render(str, locals, function(err, html){
32+
if (err) return done(err);
33+
html.should.eql('<p>Tobi</p>user_template_name');
34+
return done();
35+
});
36+
});
37+
});
38+
};

0 commit comments

Comments
 (0)