Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Compiling a template as a CommonJS module returns a function that must be hydrated with a Dust: var tmpl = require('./my/tmpl.js')(dust); Once you have a handle on that object, you can either invoke it directly: tmpl(context, function(err, out) { ... }) tmpl(context).on('data', ...) Or you can pass it to `dust.render`: dust.render(tmpl, ...) Or you can access the compiled template if you need to do something with it: tmpl.template; // function body_0(chunk, context, bodies, params) { ... }
- Loading branch information
Seth Kinast
committed
Apr 15, 2015
1 parent
7775a86
commit 815cfc9
Showing
5 changed files
with
102 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/*global describe,expect,it,beforeEach */ | ||
/*jshint evil:true*/ | ||
var dust = require('../../../'); | ||
|
||
function load(tmpl) { | ||
dust.config.cjs = true; | ||
return eval(dust.compile(tmpl, 'hello'))(dust); | ||
} | ||
|
||
describe('CommonJS template', function() { | ||
var template = "Hello {world}!", | ||
context = { world: "world" }, | ||
rendered = "Hello world!", | ||
templateName = 'hello', | ||
tmpl; | ||
|
||
beforeEach(function() { | ||
tmpl = load(template); | ||
}); | ||
|
||
it('can be invoked to render', function() { | ||
tmpl(context, function(err, out) { | ||
expect(out).toEqual(rendered); | ||
}); | ||
}); | ||
|
||
it('can be invoked to stream', function(done) { | ||
tmpl(context).on('data', function(out) { | ||
expect(out).toEqual(rendered); | ||
done(); | ||
}); | ||
}); | ||
|
||
it('can be passed to dust.render', function() { | ||
dust.render(tmpl, context, function(err, out) { | ||
expect(out).toEqual(rendered); | ||
}); | ||
}); | ||
|
||
it('can be passed to dust.stream', function(done) { | ||
dust.stream(tmpl, context).on('data', function(out) { | ||
expect(out).toEqual(rendered); | ||
done(); | ||
}); | ||
}); | ||
|
||
it('has a template property that can be passed to dust.render', function() { | ||
expect(tmpl.template.templateName).toEqual(templateName); | ||
dust.render(tmpl.template, context, function(err, out) { | ||
expect(out).toEqual(rendered); | ||
}); | ||
}); | ||
|
||
it('has a name that can be passed to dust.render', function() { | ||
dust.render(templateName, context, function(err, out) { | ||
expect(out).toEqual(rendered); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters