# all files / keystone/lib/core/ render.js

21.88% Statements 7/32
0% Branches 0/30
0% Functions 0/4
21.88% Lines 7/32

    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
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    1131×
    1×
    1×
    1×

    1×

    1×

    1×
     var _ = require('lodash');
    var cloudinary = require('cloudinary');
    var fs = require('fs');
    var jade = require('jade');

    /**
     * Renders a Keystone View
     *
     * @api private
     */

    var templateCache = {};

    function render (req, res, view, ext) {

    	var keystone = this;
    	var templatePath = __dirname + '/../../admin/server/templates/' + view + '.jade';
    	var jadeOptions = {
    		filename: templatePath,
    		pretty: keystone.get('env') !== 'production',
    	};
    	var compileTemplate = function () {
    		return jade.compile(fs.readFileSync(templatePath, 'utf8'), jadeOptions);
    	};
    	var template = templateCache[view] || (templateCache[view] = compileTemplate());

    	if (!res.req.flash) {
    		console.error('\nKeystoneJS Runtime Error:\n\napp must have flash middleware installed. Try adding "connect-flash" to your express instance.\n');
    		process.exit(1);
    	}
    	var flashMessages = {
    		info: res.req.flash('info'),
    		success: res.req.flash('success'),
    		warning: res.req.flash('warning'),
    		error: res.req.flash('error'),
    		hilight: res.req.flash('hilight'),
    	};

    	var lists = {};
    	_.forEach(keystone.lists, function (list, key) {
    		lists[key] = list.getOptions();
    	});

    	var locals = {
    		_: _,
    		env: keystone.get('env'),
    		brand: keystone.get('brand'),
    		appversion: keystone.get('appversion'),
    		nav: keystone.nav,
    		messages: _.some(flashMessages, function (msgs) { return msgs.length; }) ? flashMessages : false,
    		lists: lists,
    		userModel: keystone.get('user model'),
    		user: req.user,
    		title: 'Keystone',
    		signout: keystone.get('signout url'),
    		adminPath: '/' + keystone.get('admin path'),
    		backUrl: keystone.get('back url') || '/',
    		section: {},
    		version: keystone.version,
    		csrf_header_key: keystone.security.csrf.CSRF_HEADER_KEY,
    		csrf_token_key: keystone.security.csrf.TOKEN_KEY,
    		csrf_token_value: keystone.security.csrf.getToken(req, res),
    		csrf_query: '&' + keystone.security.csrf.TOKEN_KEY + '=' + keystone.security.csrf.getToken(req, res),
    		ga: {
    			property: keystone.get('ga property'),
    			domain: keystone.get('ga domain'),
    		},
    		wysiwygOptions: {
    			enableImages: keystone.get('wysiwyg images') ? true : false,
    			enableCloudinaryUploads: keystone.get('wysiwyg cloudinary images') ? true : false,
    			enableS3Uploads: keystone.get('wysiwyg s3 images') ? true : false,
    			additionalButtons: keystone.get('wysiwyg additional buttons') || '',
    			additionalPlugins: keystone.get('wysiwyg additional plugins') || '',
    			additionalOptions: keystone.get('wysiwyg additional options') || {},
    			overrideToolbar: keystone.get('wysiwyg override toolbar'),
    			skin: keystone.get('wysiwyg skin') || 'keystone',
    			menubar: keystone.get('wysiwyg menubar'),
    			importcss: keystone.get('wysiwyg importcss') || '',
    		},
    	};

    	// view-specific extensions to the local scope
    	_.extend(locals, ext);

    	// add cloudinary locals if configured
    	if (keystone.get('cloudinary config')) {
    		try {
    			var cloudinaryUpload = cloudinary.uploader.direct_upload();
    			locals.cloudinary = {
    				cloud_name: keystone.get('cloudinary config').cloud_name,
    				api_key: keystone.get('cloudinary config').api_key,
    				timestamp: cloudinaryUpload.hidden_fields.timestamp,
    				signature: cloudinaryUpload.hidden_fields.signature,
    				prefix: keystone.get('cloudinary prefix') || '',
    				folders: keystone.get('cloudinary folders'),
    				uploader: cloudinary.uploader,
    			};
    			locals.cloudinary_js_config = cloudinary.cloudinary_js_config();
    		} catch (e) {
    			if (e === 'Must supply api_key') {
    				throw new Error('Invalid Cloudinary Config Provided\n\n'
    					+ 'See http://keystonejs.com/docs/configuration/#services-cloudinary for more information.');
    			} else {
    				throw e;
    			}
    		}
    	}

    	res.send(template(locals));
    }

    module.exports = render;