forked from jfparadis/requirejs-mustache
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstache.js
More file actions
78 lines (71 loc) · 2.65 KB
/
stache.js
File metadata and controls
78 lines (71 loc) · 2.65 KB
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
// RequireJS Mustache template plugin
// http://github.com/jfparadis/requirejs-mustache
//
// An alternative to https://github.com/millermedeiros/requirejs-hogan-plugin
//
// Using Mustache Logic-less templates at http://mustache.github.com
// Using and RequireJS text.js at http://requirejs.org/docs/api.html#text
// @author JF Paradis
// @version 0.0.3
//
// Released under the MIT license
//
// Usage:
// require(['backbone', 'stache!mytemplate'], function (Backbone, mytemplate) {
// return Backbone.View.extend({
// initialize: function(){
// this.render();
// },
// render: function(){
// this.$el.html(mytemplate({message: 'hello'}));
// });
// });
//
// Configuration: (optional)
// require.config({
// stache: {
// extension: '.stache' // default = '.html'
// }
// });
/*jslint nomen: true */
/*global define: false */
define(['text', 'mustache'], function (text, Mustache) {
'use strict';
var sourceMap = {},
buildMap = {},
buildTemplateSource = "define('{pluginName}!{moduleName}', ['mustache'], function (Mustache) { var template = '{content}'; Mustache.parse( template ); return function( view ) { return Mustache.render( template, view ); } });\n";
return {
version: '0.0.3',
load: function (moduleName, parentRequire, onload, config) {
if (buildMap[moduleName]) {
onload(buildMap[moduleName]);
} else {
var ext = (config.stache && config.stache.extension) || '.html';
var path = (config.stache && config.stache.path) || '';
text.load(path + moduleName + ext, parentRequire, function (source) {
if (config.isBuild) {
sourceMap[moduleName] = source;
onload();
} else {
Mustache.parse(source);
buildMap[moduleName] = function( view ) {
return Mustache.render( source, view );
};
onload(buildMap[moduleName]);
}
}, config);
}
},
write: function (pluginName, moduleName, write, config) {
var source = sourceMap[moduleName],
content = source && text.jsEscape(source);
if (content) {
write.asModule(pluginName + '!' + moduleName,
buildTemplateSource
.replace('{pluginName}', pluginName)
.replace('{moduleName}', moduleName)
.replace('{content}', content));
}
}
};
});