Skip to content

Commit

Permalink
Merge pull request #408 from jimmyhchan/compileFnNameless
Browse files Browse the repository at this point in the history
dust.compileFn no longer requires the name to be defined.
  • Loading branch information
prashn64 committed Jan 19, 2014
2 parents 6a156e1 + 81ab590 commit b03b517
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 deletions.
9 changes: 5 additions & 4 deletions lib/compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@


compiler.compile = function(source, name) {
// if compile is called from compileFn via renderSource, name parameter can be ignored,
// as the templates will be rendered immediately and need not be stored in cache, but if
// compile is called directly, the template will be cached with its name, so name is mandatory.
// Only renderSource passes null as name
// the name parameter is optional.
// this can happen for templates that are rendered immediately (renderSource which calls compileFn) or
// for templates that are compiled as a callable (compileFn)
//
// for the common case (using compile and render) a name is required so that templates will be cached by name and rendered later, by name.
if (!name && name !== null) {
dust.log(new Error("Template name parameter cannot be undefined when calling dust.compile"), 'ERROR');
}
Expand Down
6 changes: 4 additions & 2 deletions lib/dust.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,13 @@
};

dust.renderSource = function(source, context, callback) {
//passing null, so that 'compile' knows that template has to be compiled but not to be stored in cache
return dust.compileFn(source, null)(context, callback);
return dust.compileFn(source)(context, callback);
};

dust.compileFn = function(source, name) {
// name is optional. When name is not provided the template can only be rendered using the callable returned by this function.
// If a name is provided the compiled template can also be rendered by name.
name = name || null;
var tmpl = dust.loadSource(dust.compile(source, name));
return function(context, callback) {
var master = callback ? new Stub(callback) : new Stream();
Expand Down
15 changes: 15 additions & 0 deletions test/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,21 @@ exports.coreSetup = function(suite, auto) {
});
});

suite.test("compileFn", function() {
var unit = this,
tmpl = dust.compileFn('Hello World');
tmpl({}, function(err, out) {
try {
unit.ifError(err);
unit.equals(out, "Hello World");
} catch(err) {
unit.fail(err);
return;
}
unit.pass();
});
});

suite.test("renderSource (stream)", function() {
var unit = this;
dust.renderSource('Hello World', {}).on('data', function(data) {
Expand Down

0 comments on commit b03b517

Please sign in to comment.