Skip to content

Commit

Permalink
Merge pull request #655 from sethkinast/examples/commonjs
Browse files Browse the repository at this point in the history
Update CommonJS example to make use of new onLoad behavior
  • Loading branch information
sethkinast committed Apr 30, 2015
2 parents 962081f + 5deb0a7 commit 325f4a9
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 15 deletions.
6 changes: 3 additions & 3 deletions examples/commonjs/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ var index = requireTemplate('index');

console.log("The name of the required template is", index.template.templateName);

console.log("You can render a template");
// You can render a template
index({ mood: "happy" }, function(err, out) {
if(err) {
console.error(err);
Expand All @@ -12,7 +12,7 @@ index({ mood: "happy" }, function(err, out) {
}
});

console.log("Or stream it");
index({ mood: "happy" }).on('data', function(data) {
// You can also stream it
index({ mood: "streamy" }).on('data', function(data) {
console.log(data);
});
2 changes: 1 addition & 1 deletion examples/commonjs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "Simple example of using CommonJS modules with Dust in Node",
"main": "app.js",
"dependencies": {
"dustjs-linkedin": "^2.7.0"
"dustjs-linkedin": "^2.7.1"
},
"scripts": {
"start": "node app.js",
Expand Down
28 changes: 17 additions & 11 deletions examples/commonjs/requireTemplate.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,28 @@ var path = require('path'),

var dust = require('dustjs-linkedin');

// Add any extra helpers, do dust config, etc...
// Add any extra helpers, do dust config, etc., in a single location
// This helper just uppercases anything inside its body
dust.helpers.shout = function(chunk, context, bodies, params) {
return chunk.tap(function(data) {
return data.toUpperCase();
}).render(bodies.block, context).untap();
};

// Adding an `onLoad` function lets us load partials
dust.onLoad = function(templateName, callback) {
requireTemplate(templateName);
callback();
};

function requireTemplate(name) {
var tmpl = require(root(name));
return tmpl(dust);
// Here's our app-specific logic
function requireTemplate(name, callback) {
var tmpl;
try {
tmpl = require(root(name))(dust);
} catch(e) {
if (callback) { callback(e); } else { throw e; }
}
if (callback) {
callback(null, tmpl);
} else {
return tmpl;
}
}

module.exports = requireTemplate;
// Adding an `onLoad` function lets us load partials
module.exports = dust.onLoad = requireTemplate;

0 comments on commit 325f4a9

Please sign in to comment.