-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
118 lines (105 loc) · 3.21 KB
/
index.js
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
113
114
115
116
117
118
'use strict';
var debug = require('debug')('base:engines');
var utils = require('./utils');
module.exports = function(options) {
return function baseEngines(app) {
if (!utils.isValid(app, 'base-engines', ['app', 'collection', 'views', 'list'])) {
return;
}
if (typeof this._ === 'undefined') {
utils.define(this, '_', {});
}
this.engines = this.engines || {};
this._.engines = new utils.Engines(this.engines);
this._.helpers = {
async: {},
sync: {}
};
/**
* Register a view engine callback `fn` as `ext`. Calls `.setEngine`
* and `.getEngine` internally.
*
* ```js
* app.engine('hbs', require('engine-handlebars'));
*
* // using consolidate.js
* var engine = require('consolidate');
* app.engine('jade', engine.jade);
* app.engine('swig', engine.swig);
*
* // get a registered engine
* var swig = app.engine('swig');
* ```
* @name .engine
* @param {String|Array} `exts` String or array of file extensions.
* @param {Function|Object} `fn` or `settings`
* @param {Object} `settings` Optionally pass engine options as the last argument.
* @api public
*/
this.define('engine', function(exts, fn, settings) {
if (arguments.length === 1 && typeof exts === 'string') {
return this.getEngine(exts);
}
if (!Array.isArray(exts) && typeof exts !== 'string') {
throw new TypeError('expected engine ext to be a string or array.');
}
if (settings === 'function') {
return this.engine(exts, settings, fn);
}
utils.arrayify(exts).forEach(function(ext) {
this.setEngine(ext, fn, settings);
}.bind(this));
return this;
});
/**
* Register engine `ext` with the given render `fn` and/or `settings`.
*
* ```js
* app.setEngine('hbs', require('engine-handlebars'), {
* delims: ['<%', '%>']
* });
* ```
* @name .setEngine
* @param {String} `ext` The engine to set.
* @api public
*/
this.define('setEngine', function(ext, fn, settings) {
debug('registering engine "%s"', ext);
settings = settings || {};
ext = utils.formatExt(ext);
if (settings.default === true) {
this._.engines.defaultEngine = ext;
}
this._.engines.setEngine(ext, fn, settings);
return this;
});
/**
* Get registered engine `ext`.
*
* ```js
* app.engine('hbs', require('engine-handlebars'));
* var engine = app.getEngine('hbs');
* ```
* @name .getEngine
* @param {String} `ext` The engine to get.
* @api public
*/
this.define('getEngine', function(ext, fallback) {
debug('getting engine "%s"', ext);
if (!utils.isString(ext)) {
ext = this.options['view engine']
|| this.options.viewEngine
|| this.options.engine;
}
if (utils.isString(ext)) {
ext = utils.formatExt(ext);
var engine = this._.engines.getEngine(ext);
if (!engine && this.options.engine && fallback !== false) {
return this.getEngine(this.options.engine, false);
}
return engine;
}
});
return baseEngines;
};
};