all files / express-stormpath/lib/helpers/ render.js

68% Statements 17/25
50% Branches 8/16
100% Functions 2/2
68% Lines 17/25
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65          27×   27×               27×                                           27× 27× 27×   27× 27× 27×   27×     27×                    
'use strict';
 
var path = require('path');
 
var jade = require('jade');
var mixin = require('utils-merge');
 
var viewCache = {};
 
function renderJade(filepath, locals) {
  var env = process.env.NODE_ENV;
 
  Iif (env === 'production') {
    if (!viewCache[filepath]) {
      viewCache[filepath] = jade.compileFile(filepath);
    }
 
    return viewCache[filepath](locals);
  }
 
  return jade.renderFile(filepath, locals);
}
 
/**
 * Render a view using app locals.
 *
 * By default, use Jade as it is necessary because our library can't rely
 * on the developer using Jade view as well -- so this allows us to use
 * Jade templates for our library views, without negatively affecting the
 * developer's application.
 *
 * If, however, the developer has supplied a render handler in their settings,
 * then we'll go ahead and use that render function instead.
 *
 * @method
 * @private
 *
 * @param {String} view - The filename to the view to render.
 * @param {Object} res - The http response.
 * @param {Object} options - The locals which will be supplied to the view
 *   during rendering.
 */
module.exports = function (req, res, view, options) {
  var config = req.app.get('stormpathConfig');
  var extension = path.extname(view);
  var filename = path.basename(view, extension);
 
  options = options || {};
  mixin(options, res.locals);
  mixin(options, config.templateContext || {});
 
  Eif (!extension && (filename === view)) {
    // This means that we have received a default config option, such as
    // 'login' - just continue to render our default page.
    res.send(renderJade(path.join(path.dirname(__dirname), 'views', view + '.jade'), options));
  } else if (extension === '.jade') {
    res.send(renderJade(view, options));
  } else if (extension) {
    // Delegate to the view engine.
    res.render(view, options);
  } else {
    throw new Error('Unexpected view option: "' + view + '".  Please see documentation for express-stormpath');
  }
};