Skip to content

Commit

Permalink
Merge pull request #278 from chiragpat/master
Browse files Browse the repository at this point in the history
Passing in template name to dust compile.
  • Loading branch information
doowb authored Sep 18, 2017
2 parents 81a3ec6 + 03219a2 commit c9a94a5
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 1 deletion.
7 changes: 6 additions & 1 deletion lib/consolidate.js
Original file line number Diff line number Diff line change
Expand Up @@ -511,7 +511,12 @@ exports.dust.render = function(str, options, fn){
};

try {
var tmpl = cache(options) || cache(options, engine.compileFn(str));
var templateName;
if (options.filename) {
templateName = options.filename.replace(new RegExp('^' + views + '/'), '').replace(new RegExp('\\.' + ext), '');
}

var tmpl = cache(options) || cache(options, engine.compileFn(str, templateName));
tmpl(options, fn);
} catch (err) {
fn(err);
Expand Down
1 change: 1 addition & 0 deletions test/consolidate.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ require('./shared').test('hogan');
require('./shared/partials').test('hogan');
require('./shared').test('dust');
require('./shared/partials').test('dust');
require('./shared/dust').test('dust');
require('./shared').test('handlebars');
require('./shared/partials').test('handlebars');
require('./shared/helpers').test('handlebars');
Expand Down
2 changes: 2 additions & 0 deletions test/fixtures/dust/user_template_name.dust
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<p>{user.name}</p>
{@templateName/}
38 changes: 38 additions & 0 deletions test/shared/dust.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*eslint-env node, mocha */
var cons = require('../../');
var fs = require('fs');

// var should = require('should');

exports.test = function(name) {
var user = { name: 'Tobi' };

describe(name, function(){
// Use case: return upper case string.
it('should support fetching template name from the context', function(done) {
var viewsDir = 'test/fixtures/' + name;
var templatePath = viewsDir + '/user_template_name.' + name;
var str = fs.readFileSync(templatePath).toString();

var locals = {
user: user,
views: viewsDir,
filename: templatePath
};

if (name === 'dust') {
var dust = require('dustjs-helpers');
dust.helpers.templateName = function(chunk, context, bodies, params) {
return chunk.write(context.getTemplateName());
};
cons.requires.dust = dust;
}

cons[name].render(str, locals, function(err, html){
if (err) return done(err);
html.should.eql('<p>Tobi</p>user_template_name');
return done();
});
});
});
};

0 comments on commit c9a94a5

Please sign in to comment.