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

GH-266 store rendered template in global #274

Merged
merged 4 commits into from
Jun 2, 2013
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
12 changes: 7 additions & 5 deletions lib/dust.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ dust.register = function(name, tmpl) {

dust.render = function(name, context, callback) {
var chunk = new Stub(callback).head;
dust.load(name, chunk, Context.wrap(context)).end();
dust.load(name, chunk, Context.wrap(context, name)).end();
};

dust.stream = function(name, context) {
var stream = new Stream();
dust.nextTick(function() {
dust.load(name, stream.head, Context.wrap(context)).end();
dust.load(name, stream.head, Context.wrap(context, name)).end();
});
return stream;
};
Expand All @@ -39,7 +39,7 @@ dust.compileFn = function(source, name) {
return function(context, callback) {
var master = callback ? new Stub(callback) : new Stream();
dust.nextTick(function() {
tmpl(master.head, Context.wrap(context)).end();
tmpl(master.head, Context.wrap(context, name)).end();
});
return master;
};
Expand Down Expand Up @@ -131,11 +131,13 @@ dust.makeBase = function(global) {
return new Context(new Stack(), global);
};

Context.wrap = function(context) {
Context.wrap = function(context, name) {
if (context instanceof Context) {
return context;
}
return new Context(new Stack(context));
var global= {};
global.__template_name__ = name;
return new Context(new Stack(context), global);
};

Context.prototype.get = function(key) {
Expand Down
14 changes: 14 additions & 0 deletions test/jasmine-test/spec/coreTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,20 @@ var coreTests = [
context: {},
expected: "Hello World!",
message: "should test basic text rendering"
},
{
name: "global_template",
source: '{#helper foo="bar" boo="boo"} {/helper}',
context: { "helper": function(chunk, context, bodies, params) { return chunk.write(context.global.__template_name__); } },
expected: "global_template",
message: "should render the template name"
},
{
name: "apps/test/foo.tl&v=0.1",
source: '{#helper foo="bar" boo="boo" template="tl/apps/test"} {/helper}',
context: { "helper": function(chunk, context, bodies, params) { return chunk.write(context.global.__template_name__); } },
expected: "apps/test/foo.tl&v=0.1",
message: "should render the template name with paths"
},
{
name: "reference",
Expand Down