Skip to content

Commit

Permalink
Passing in template name to dust compile.
Browse files Browse the repository at this point in the history
This will ensure when context.getTemplateName is called in a base template the appropriate name is available
  • Loading branch information
chiragpat committed Apr 30, 2017
1 parent 894b0de commit 03219a2
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 @@ -383,7 +383,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 @@ -19,6 +19,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 03219a2

Please sign in to comment.