diff --git a/index.js b/index.js index 17d1aa67..8b81a6dd 100644 --- a/index.js +++ b/index.js @@ -6,8 +6,8 @@ var vm = require('vm'), // use Node equivalents for some Dust methods var context = vm.createContext({dust: dust}); -dust.loadSource = function(source, path) { - return vm.runInContext(source, context, path); +dust.loadSource = function(source) { + return vm.runInContext(source, context); }; dust.nextTick = process.nextTick; diff --git a/lib/compiler.js b/lib/compiler.js index a0921bd4..021c65a5 100644 --- a/lib/compiler.js +++ b/lib/compiler.js @@ -22,9 +22,6 @@ // 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) { - throw new Error('Template name parameter cannot be undefined when calling dust.compile'); - } try { var ast = filterAST(parse(source)); @@ -164,18 +161,21 @@ auto: 'h' }, escapedName = dust.escapeJs(name), - body_0 = 'function(dust){dust.register(' + - (name ? '"' + escapedName + '"' : 'null') + ',' + - compiler.compileNode(context, ast) + - ');' + - compileBlocks(context) + - compileBodies(context) + - 'return body_0;}'; + compiled = ''; + + compiler.compileNode(context, ast); + + if(name) { + compiled += 'dust.register("' + escapedName + '",body_0);'; + } + + compiled += compileBlocks(context) + + compileBodies(context); if(dust.config.amd) { - return 'define("' + escapedName + '",["dust.core"],' + body_0 + ');'; + return 'define("' + escapedName + '",["dust.core"],function(dust){' + compiled + 'return body_0});'; } else { - return '(' + body_0 + ')(dust);'; + return '(function(dust){' + compiled + 'return body_0})(dust);'; } } @@ -427,13 +427,19 @@ function(str) { return '"' + escapeToJsSafeString(str) + '"';} : JSON.stringify; + function renderSource(source, context, callback) { + var tmpl = dust.loadSource(dust.compile(source)); + if(callback) { + return dust.render(tmpl, context, callback); + } else { + return dust.stream(tmpl, context); + } + } + // expose compiler methods - dust.compile = compiler.compile; - dust.filterNode = compiler.filterNode; - dust.optimizers = compiler.optimizers; - dust.pragmas = compiler.pragmas; - dust.compileNode = compiler.compileNode; - dust.nodes = compiler.nodes; + dust.compiler = compiler; + dust.compile = dust.compiler.compile; + dust.renderSource = renderSource; return compiler; diff --git a/lib/dust.js b/lib/dust.js index 70c8f8f1..b56baccd 100644 --- a/lib/dust.js +++ b/lib/dust.js @@ -84,24 +84,25 @@ if (!name) { return; } + tmpl.templateName = name; dust.cache[name] = tmpl; }; dust.render = function(nameOrTemplate, context, callback) { var chunk = new Stub(callback).head; try { - dust.load(nameOrTemplate, chunk, Context.wrap(context, nameOrTemplate)).end(); + load(nameOrTemplate, chunk, context).end(); } catch (err) { chunk.setError(err); } }; - dust.stream = function(name, context) { + dust.stream = function(nameOrTemplate, context) { var stream = new Stream(), chunk = stream.head; dust.nextTick(function() { try { - dust.load(name, stream.head, Context.wrap(context, name)).end(); + load(nameOrTemplate, chunk, context).end(); } catch (err) { chunk.setError(err); } @@ -109,37 +110,14 @@ return stream; }; - dust.renderSource = function(source, context, callback) { - return dust.compileFn(source)(context, callback); - }; - - /** - * Compile a template to an invokable function. - * If `name` is provided, also registers the template under `name`. - * @param source {String} template source - * @param [name] {String} template name - * @return {Function} has the signature `fn(context, cb)` - */ - dust.compileFn = function(source, name) { - name = name || null; - var tmpl = dust.loadSource(dust.compile(source, name)); - return function(context, callback) { - var master = callback ? new Stub(callback) : new Stream(); - dust.nextTick(function() { - if(typeof tmpl === 'function') { - tmpl(master.head, Context.wrap(context, name)).end(); - } else { - dust.log(new Error('Template `' + name + '` could not be loaded'), ERROR); - } - }); - return master; - }; - }; - - dust.load = function(nameOrTemplate, chunk, context) { - var tmpl = nameOrTemplate.__dustBody ? nameOrTemplate : dust.cache[nameOrTemplate]; + function load(nameOrTemplate, chunk, context) { + if(!nameOrTemplate) { + return chunk.setError(new Error('No template or template name provided to render')); + } + var tmpl = nameOrTemplate.__dustBody ? nameOrTemplate : dust.cache[nameOrTemplate], + templateName = tmpl.templateName; if (tmpl) { - return tmpl(chunk, context); + return tmpl(chunk, Context.wrap(context, templateName)); } else { if (dust.onLoad) { return chunk.map(function(chunk) { @@ -150,13 +128,13 @@ if (!dust.cache[nameOrTemplate]) { dust.loadSource(dust.compile(src, nameOrTemplate)); } - dust.cache[nameOrTemplate](chunk, context).end(); + dust.cache[nameOrTemplate](chunk, Context.wrap(context, nameOrTemplate)).end(); }); }); } return chunk.setError(new Error('Template Not Found: ' + nameOrTemplate)); } - }; + } dust.loadSource = function(source) { /*jshint evil:true*/ @@ -173,7 +151,7 @@ dust.nextTick = (function() { return function(callback) { - setTimeout(callback,0); + setTimeout(callback, 0); }; } )(); @@ -848,11 +826,11 @@ // Load the partial after getting its name and end the async chunk return this.capture(elem, context, function(name, chunk) { context.templateName = name; - dust.load(name, chunk, context).end(); + load(name, chunk, context).end(); }); } else { context.templateName = elem; - return dust.load(elem, this, context); + return load(elem, this, context); } }; diff --git a/test/core.js b/test/core.js index 666f45f2..26545cc4 100644 --- a/test/core.js +++ b/test/core.js @@ -49,21 +49,6 @@ 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) { diff --git a/test/jasmine-test/spec/coreTests.js b/test/jasmine-test/spec/coreTests.js index 03bc7ca9..4fe07eff 100755 --- a/test/jasmine-test/spec/coreTests.js +++ b/test/jasmine-test/spec/coreTests.js @@ -69,12 +69,6 @@ var coreTests = [ expected: '', message: "should test the stream rendering" }, - { - name: undefined, - source: "compilation_failure", - error: "Template name parameter cannot be undefined when calling dust.compile", - message: "if the name is not there compilation should be failed, unless it is called from renderSource" - }, { name: "hello_world", source: "Hello World!", diff --git a/test/jasmine-test/spec/renderTestSpec.js b/test/jasmine-test/spec/renderTestSpec.js index 21708c75..57b37b44 100644 --- a/test/jasmine-test/spec/renderTestSpec.js +++ b/test/jasmine-test/spec/renderTestSpec.js @@ -32,7 +32,7 @@ describe ('Pipe', function() { }); function prepare(test) { - dust.config = test.config || { whitespace: false }; + dust.config = test.config || { whitespace: false, amd: false, cache: true }; dust.loadSource(dust.compile(test.source, test.name)); context = test.context; if (test.base) {