Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue #407, compileFn does not require a name. Add unit test as well #408

Merged
merged 1 commit into from
Jan 19, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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