Skip to content
Myr

Added app.register() for template engine mapping. Closes #390

Before adoption: this commit came in with the history the project adopted, so no landing records it.

This is not exactly what you requested, but I think this is a better approach. With old Express I used to have "foo.ejs.html" "foo.haml.html" etc, but that is super verbose. This will allow for non-authors to support non-compliant engines without modifying the original module, as well as having things like .html render with jade

authored by Tj Holowaychuk · committed by Tj Holowaychuk · parent da8c0cd · Browse files · 4 files+60 added−0 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

4 files+60 added−0 removed

  • tests:+15 added−0 removed
  • other:+45 added−0 removed

4 files+60 added−0 removed

modified:lib/express/server.js+12 added
147 unmodified lines
148 return this;149};150151/**152 * See: view.register.153 *154 * @return {Server} for chaining155 * @api public156 */157158Server.prototype.register = function(){159 view.register.apply(this, arguments);160 return this;161};162163/**164 * Register the given view helpers `obj`. This method165 * can be called several times to apply additional helpers.166 *167 * @param {Object} obj
124 unmodified lines
modified:lib/express/view.js+33 added
57 unmodified lines
58 return app.set('views') || process.cwd() + '/views';59}6061/**62 * Register the given template engine `exports`63 * as `ext`. For example we may wish to map ".html"64 * files to jade:65 *66 * app.register('.html', require('jade'));67 *68 * This is also useful for libraries that may not69 * match extensions correctly. For example my haml.js70 * library is installed from npm as "hamljs" so instead71 * of layout.hamljs, we can register the engine as ".haml":72 *73 * app.register('.haml', require('haml-js'));74 *75 * For engines that do not comply with the Express76 * specification, we can also wrap their api this way.77 *78 * app.register('.foo', {79 * render: function(str, options) {80 * // perhaps their api is81 * // foo.toHTML(str, options);82 * }83 * });84 *85 * @param {String} ext86 * @param {Object} obj87 * @api public88 */8990exports.register = function(ext, exports) {91 cache[ext] = exports;92};9394/**95 * Render `view` partial with the given `options`.96 *97 * Options:98 * - `object` Single object with name derived from the view (unless `as` is present)
202 unmodified lines
added:test/fixtures/foo.bar+1 added
1p This is actually jade :)
modified:test/view.test.js+14 added
438 unmodified lines
439 { body: '<h1>tj</h1>\n<p>tj@vision-media.ca</p>' });440 assert.response(app,441 { url: '/video' },442 { body: '<h1>keyboard cat</h1>' });443 },444 445 'test .register()': function(assert){446 var app = create();447 448 app.register('.bar', require('jade'));449 450 app.get('/', function(req, res, next){451 res.render('foo.bar', { layout: false });452 });453 454 assert.response(app,455 { url: '/' },456 { body: '<p>This is actually jade :)</p>' });457 }458};