Skip to content
Browse files

- initial commit

  • Loading branch information...
0 parents commit 1c68690c19d6fdaf30b0d3b689f1cff1b401bad9 @kalmiz committed Mar 7, 2011
Showing with 104 additions and 0 deletions.
  1. +1 −0 .gitignore
  2. +1 −0 index.js
  3. +84 −0 lib/express-dust.js
  4. +18 −0 package.json
1 .gitignore
@@ -0,0 +1 @@
+node_modules/*
1 index.js
@@ -0,0 +1 @@
+module.exports = require('./lib/express-dust.js');
84 lib/express-dust.js
@@ -0,0 +1,84 @@
+/**
+ * Module dependencies.
+ */
+var fs = require('fs'),
+ dust = require('dust');
+ http = require('http'),
+ res = http.ServerResponse.prototype;
+
+function render(file, context, next) {
+ if (false === context.get('__cache')) {
+ dust.onLoad(file, function(err, src) {
+ if (err) next(err);
+ else {
+ dust.renderSource(src, context, next);
+ }
+ });
+ } else {
+ dust.render(file, context, next);
+ }
+};
+
+res.render = function(view, opts, fn) {
+ // support callback function as second arg
+ if (typeof opts === 'function') {
+ fn = opts, opts = null;
+ }
+ var self = this,
+ app = this.app,
+ helpers = app.viewHelpers,
+ dynamicHelpers = app.dynamicViewHelpers,
+ engine = app.set('view engine') || 'dust',
+ viewOptions = app.set('view options'),
+ props = {};
+
+ props.__cache = app.set('cache views');
+ props.__dirname = (app.set('views') || process.cwd() + '/views');
+ props.__filename = view + (view.indexOf('.') > -1 ? '' : '.' + engine);
+
+ var context = dust.makeBase(opts || {})
+ .push(viewOptions)
+ .push(props)
+ .push(helpers);
+ // Dynamic helper support
+ if (false !== context.get('dynamicHelpers')) {
+ // cache
+ if (!this.__dynamicHelpers) {
+ this.__dynamicHelpers = {};
+ for (var key in dynamicHelpers) {
+ this.__dynamicHelpers[key] = dynamicHelpers[key].call(
+ this.app,
+ this.req,
+ this);
+ }
+ }
+ context = context.push(this.__dynamicHelpers);
+ }
+
+ render(view, context, function (err, out) {
+ if (fn) fn(err, out);
+ else {
+ if (err) self.req.next(err);
+ else self.send(out);
+ }
+ });
+};
+
+module.exports = {
+ render: function(view, context, next) {
+ render(view, dust.makeBase(context), next);
+ },
+ //disable whitespace compression
+ disableCompression: function() {
+ dust.optimizers.format = function(ctx, node) { return node };
+ },
+ register: function(app) {
+ dust.onLoad = function(view, next) {
+ fs.readFile(
+ (app.set('views') || process.cwd() + '/views') + '/' + view +
+ (view.indexOf('.') > -1 ? '' : '.' + (app.set('view engine') || 'dust')),
+ 'utf8', next
+ );
+ }
+ }
+}
18 package.json
@@ -0,0 +1,18 @@
+{
+ "name": "express-dust",
+ "description": "Express.js view engine adapter for Dust template engine",
+ "version": "0.0.1",
+ "author": {
+ "name": "Zoltan Kalmar",
+ "email": "kalmiz@gmail.com",
+ "url": "http://kalmi.eu"
+ },
+ "keywords": [
+ "dust",
+ "express",
+ "template engine"
+ ],
+ "dependencies": {
+ "dust": "latest"
+ }
+}

0 comments on commit 1c68690

Please sign in to comment.
Something went wrong with that request. Please try again.