This repository has been archived by the owner on Jul 16, 2024. It is now read-only.
forked from rendrjs/rendr-handlebars
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
68 lines (57 loc) · 2.2 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
module.exports = function(options, Handlebars) {
if (Handlebars['default']) {
// If we only have the Handlebars runtime available, use that here.
// Until Handlebars 3, we have to use 'default' instead of just requiring 'handlebars'.
Handlebars = Handlebars['default'];
}
var localExports = {},
templateFinder = options.templateFinder || require('./shared/templateFinder')(Handlebars);
/**
* Export the `Handlebars` object, so other modules can add helpers, partials, etc.
*/
localExports.Handlebars = Handlebars;
/**
* `getTemplate` is available on both client and server.
*/
localExports.getTemplate = templateFinder.getTemplate;
/**
* Expose `templatePatterns` for manipulating how `getTemplate` finds templates.
*/
localExports.templatePatterns = templateFinder.templatePatterns;
/**
* The default pattern `/.+/` is very greedy; it matches anything, including nested paths.
* To add rules that should match before this default rule, `unshift` them from this array.
*/
localExports.templatePatterns.push({pattern: /.+/, src: options.entryPath + 'app/templates/compiledTemplates'})
/**
* `getLayout` should only be used on the server.
*/
if (typeof window === 'undefined') {
// server only, "hide" it from r.js compiler
// by having require statement with variable
var serverOnlyLayoutFinderPath = './server/layoutFinder';
localExports.getLayout = require(serverOnlyLayoutFinderPath)(Handlebars).getLayout;
} else {
localExports.getLayout = function() {
throw new Error('getLayout is only available on the server.');
};
}
/**
* Register helpers, available on both client and server.
*
* Export it so other modules can register helpers as well.
*/
localExports.registerHelpers = function registerHelpers(helpersModule) {
var helpers = helpersModule(Handlebars, localExports.getTemplate);
for (var key in helpers) {
if (!helpers.hasOwnProperty(key)) continue;
Handlebars.registerHelper(key, helpers[key]);
}
};
/**
* Register the pre-bundled Rendr helpers.
*/
var rendrHelpers = require('./shared/helpers');
localExports.registerHelpers(rendrHelpers);
return localExports;
}