forked from jupiterjs/jquerymx
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathejs.js
More file actions
523 lines (499 loc) · 13.9 KB
/
ejs.js
File metadata and controls
523 lines (499 loc) · 13.9 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
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
/*jslint evil: true */
steal.plugins('jquery/view', 'jquery/lang/rsplit').then(function( $ ) {
var myEval = function(script){
eval(script);
},
chop = function( string ) {
return string.substr(0, string.length - 1);
},
rSplit = $.String.rsplit,
extend = $.extend,
isArray = $.isArray,
clean = function( content ) {
return content.replace(/\\/g, '\\\\').replace(/\n/g, '\\n').replace(/"/g, '\\"');
}
// from prototype http://www.prototypejs.org/
escapeHTML = function(content){
return content.replace(/&/g,'&')
.replace(/</g,'<')
.replace(/>/g,'>')
.replace(/"/g, '"')
.replace(/'/g, "'");
},
EJS = function( options ) {
//returns a renderer function
if ( this.constructor != EJS ) {
var ejs = new EJS(options);
return function( data, helpers ) {
return ejs.render(data, helpers);
};
}
//so we can set the processor
if ( typeof options == "function" ) {
this.template = {};
this.template.process = options;
return;
}
//set options on self
extend(this, EJS.options, options);
this.template = compile(this.text, this.type, this.name);
};
/**
* @class jQuery.EJS
*
* @plugin jquery/view/ejs
* @parent jQuery.View
* @download http://jmvcsite.heroku.com/pluginify?plugins[]=jquery/view/ejs/ejs.js
* @test jquery/view/ejs/qunit.html
*
*
* Ejs provides <a href="http://www.ruby-doc.org/stdlib/libdoc/erb/rdoc/">ERB</a>
* style client side templates. Use them with controllers to easily build html and inject
* it into the DOM.
*
* ### Example
*
* The following generates a list of tasks:
*
* @codestart html
* <ul>
* <% for(var i = 0; i < tasks.length; i++){ %>
* <li class="task <%= tasks[i].identity %>"><%= tasks[i].name %></li>
* <% } %>
* </ul>
* @codeend
*
* For the following examples, we assume this view is in <i>'views\tasks\list.ejs'</i>.
*
*
* ## Use
*
* ### Loading and Rendering EJS:
*
* You should use EJS through the helper functions [jQuery.View] provides such as:
*
* - [jQuery.fn.after after]
* - [jQuery.fn.append append]
* - [jQuery.fn.before before]
* - [jQuery.fn.html html],
* - [jQuery.fn.prepend prepend],
* - [jQuery.fn.replaceWith replaceWith], and
* - [jQuery.fn.text text].
*
* or [jQuery.Controller.prototype.view].
*
* ### Syntax
*
* EJS uses 5 types of tags:
*
* - <code><% CODE %></code> - Runs JS Code.
* For example:
*
* <% alert('hello world') %>
*
* - <code><%= CODE %></code> - Runs JS Code and writes the result into the result of the template.
* For example:
*
* <h1><%= 'hello world' %></h1>
*
* - <code><%~ CODE %></code> - Runs JS Code and writes the _escaped_ result into the result of the template.
* For example:
*
* <%~ 'hello world' %>
*
* - <code><%%= CODE %></code> - Writes <%= CODE %> to the result of the template. This is very useful for generators.
*
* <%%= 'hello world' %>
*
* - <code><%# CODE %></code> - Used for comments. This does nothing.
*
* <%# 'hello world' %>
*
* ## Hooking up controllers
*
* After drawing some html, you often want to add other widgets and plugins inside that html.
* View makes this easy. You just have to return the Contoller class you want to be hooked up.
*
* @codestart
* <ul <%= Mxui.Tabs%>>...<ul>
* @codeend
*
* You can even hook up multiple controllers:
*
* @codestart
* <ul <%= [Mxui.Tabs, Mxui.Filler]%>>...<ul>
* @codeend
*
* <h2>View Helpers</h2>
* View Helpers return html code. View by default only comes with
* [jQuery.EJS.Helpers.prototype.view view] and [jQuery.EJS.Helpers.prototype.text text].
* You can include more with the view/helpers plugin. But, you can easily make your own!
* Learn how in the [jQuery.EJS.Helpers Helpers] page.
*
* @constructor Creates a new view
* @param {Object} options A hash with the following options
* <table class="options">
* <tbody><tr><th>Option</th><th>Default</th><th>Description</th></tr>
* <tr>
* <td>url</td>
* <td> </td>
* <td>loads the template from a file. This path should be relative to <i>[jQuery.root]</i>.
* </td>
* </tr>
* <tr>
* <td>text</td>
* <td> </td>
* <td>uses the provided text as the template. Example:<br/><code>new View({text: '<%=user%>'})</code>
* </td>
* </tr>
* <tr>
* <td>element</td>
* <td> </td>
* <td>loads a template from the innerHTML or value of the element.
* </td>
* </tr>
* <tr>
* <td>type</td>
* <td>'<'</td>
* <td>type of magic tags. Options are '<' or '['
* </td>
* </tr>
* <tr>
* <td>name</td>
* <td>the element ID or url </td>
* <td>an optional name that is used for caching.
* </td>
* </tr>
* <tr>
* <td>cache</td>
* <td>true in production mode, false in other modes</td>
* <td>true to cache template.
* </td>
* </tr>
*
* </tbody></table>
*/
$.EJS = EJS;
/**
* @Prototype
*/
EJS.prototype = {
constructor: EJS,
/**
* Renders an object with extra view helpers attached to the view.
* @param {Object} object data to be rendered
* @param {Object} extra_helpers an object with additonal view helpers
* @return {String} returns the result of the string
*/
render: function( object, extraHelpers ) {
object = object || {};
this._extra_helpers = extraHelpers;
var v = new EJS.Helpers(object, extraHelpers || {});
return this.template.process.call(object, object, v);
}
};
/* @Static */
EJS.
/**
* Used to convert what's in <%= %> magic tags to a string
* to be inserted in the rendered output.
*
* Typically, it's a string, and the string is just inserted. However,
* if it's a function or an object with a hookup method, it can potentially be
* be ran on the element after it's inserted into the page.
*
* This is a very nice way of adding functionality through the view.
* Usually this is done with [jQuery.EJS.Helpers.prototype.plugin]
* but the following fades in the div element after it has been inserted:
*
* @codestart
* <%= function(el){$(el).fadeIn()} %>
* @codeend
*
* @param {String|Object|Function} input the value in between the
* write majic tags: <%= %>
* @return {String} returns the content to be added to the rendered
* output. The content is different depending on the type:
*
* * string - a bac
* * foo - bar
*/
text = function( input ) {
if ( typeof input == 'string' ) {
return input;
}
if ( input === null || input === undefined ) {
return '';
}
var hook =
(input.hookup && function( el, id ) {
input.hookup.call(input, el, id);
})
||
(typeof input == 'function' && input)
||
(isArray(input) && function( el, id ) {
for ( var i = 0; i < input.length; i++ ) {
var stub;
stub = input[i].hookup ? input[i].hookup(el, id) : input[i](el, id);
}
});
if(hook){
return "data-view-id='" + $.View.hookup(hook) + "'";
}
return input.toString ? input.toString() : "";
};
EJS.clean = function(text){
//return sanatized text
if(typeof text == 'string'){
return escapeHTML(text)
}else{
return "";
}
}
//returns something you can call scan on
var scan = function(scanner, source, block ) {
var source_split = rSplit(source, /\n/),
i=0;
for (; i < source_split.length; i++ ) {
scanline(scanner, source_split[i], block);
}
},
scanline= function(scanner, line, block ) {
scanner.lines++;
var line_split = rSplit(line, scanner.splitter),
token;
for ( var i = 0; i < line_split.length; i++ ) {
token = line_split[i];
if ( token !== null ) {
block(token, scanner);
}
}
},
makeScanner = function(left, right){
var scanner = {};
extend(scanner, {
left: left + '%',
right: '%' + right,
dLeft: left + '%%',
dRight: '%%' + right,
eeLeft : left + '%==',
eLeft: left + '%=',
cmnt: left + '%#',
cleanLeft: left+"%~",
scan : scan,
lines : 0
});
scanner.splitter = new RegExp("(" + [scanner.dLeft, scanner.dRight, scanner.eeLeft, scanner.eLeft, scanner.cleanLeft,
scanner.cmnt, scanner.left, scanner.right + '\n', scanner.right, '\n'].join(")|(").
replace(/\[/g,"\\[").replace(/\]/g,"\\]") + ")");
return scanner;
},
// compiles a template
compile = function( source, left, name ) {
source = source.replace(/\r\n/g, "\n").replace(/\r/g, "\n");
//normalize line endings
left = left || '<';
var put_cmd = "___v1ew.push(",
insert_cmd = put_cmd,
buff = new EJS.Buffer(['var ___v1ew = [];'], []),
content = '',
put = function( content ) {
buff.push(put_cmd, '"', clean(content), '");');
},
startTag = null,
empty = function(){
content = ''
};
scan( makeScanner(left, left === '[' ? ']' : '>') ,
source||"",
function( token, scanner ) {
// if we don't have a start pair
if ( startTag === null ) {
switch ( token ) {
case '\n':
content = content + "\n";
put(content);
buff.cr();
empty();
break;
case scanner.left:
case scanner.eLeft:
case scanner.eeLeft:
case scanner.cleanLeft:
case scanner.cmnt:
startTag = token;
if ( content.length > 0 ) {
put(content);
}
empty();
break;
// replace <%% with <%
case scanner.dLeft:
content += scanner.left;
break;
default:
content += token;
break;
}
}
else {
switch ( token ) {
case scanner.right:
switch ( startTag ) {
case scanner.left:
if ( content[content.length - 1] == '\n' ) {
content = chop(content);
buff.push(content, ";");
buff.cr();
}
else {
buff.push(content, ";");
}
break;
case scanner.cleanLeft :
buff.push(insert_cmd, "(jQuery.EJS.clean(", content, ")));");
break;
case scanner.eLeft:
buff.push(insert_cmd, "(jQuery.EJS.text(", content, ")));");
break;
case scanner.eeLeft:
buff.push(insert_cmd, "(jQuery.EJS.text(", content, ")));");
break;
}
startTag = null;
empty();
break;
case scanner.dRight:
content += scanner.right;
break;
default:
content += token;
break;
}
}
})
if ( content.length > 0 ) {
// Should be content.dump in Ruby
buff.push(put_cmd, '"', clean(content) + '");');
}
var template = buff.close(),
out = {
out : 'try { with(_VIEW) { with (_CONTEXT) {' + template + " return ___v1ew.join('');}}}catch(e){e.lineNumber=null;throw e;}"
};
//use eval instead of creating a function, b/c it is easier to debug
myEval.call(out,'this.process = (function(_CONTEXT,_VIEW){' + out.out + '});\r\n//@ sourceURL='+name+".js");
return out;
};
// a line and script buffer
// we use this so we know line numbers when there
// is an error.
// pre and post are setup and teardown for the buffer
EJS.Buffer = function( pre_cmd, post ) {
this.line = [];
this.script = [];
this.post = post;
// add the pre commands to the first line
this.push.apply(this, pre_cmd);
};
EJS.Buffer.prototype = {
//need to maintain your own semi-colons (for performance)
push: function() {
this.line.push.apply(this.line, arguments);
},
cr: function() {
this.script.push(this.line.join(''), "\n");
this.line = [];
},
//returns the script too
close: function() {
var stub;
if ( this.line.length > 0 ) {
this.script.push(this.line.join(''));
this.line = [];
}
stub = this.post.length && this.push.apply(this, this.post);
this.script.push(";"); //makes sure we always have an ending /
return this.script.join("");
}
};
//type, cache, folder
/**
* @attribute options
* Sets default options for all views
* <table class="options">
* <tbody><tr><th>Option</th><th>Default</th><th>Description</th></tr>
* <tr>
* <td>type</td>
* <td>'<'</td>
* <td>type of magic tags. Options are '<' or '['
* </td>
* </tr>
* <tr>
* <td>cache</td>
* <td>true in production mode, false in other modes</td>
* <td>true to cache template.
* </td>
* </tr>
* </tbody></table>
*
*/
EJS.options = {
type: '<',
ext: '.ejs'
};
/**
* @class jQuery.EJS.Helpers
* @parent jQuery.EJS
* By adding functions to jQuery.EJS.Helpers.prototype, those functions will be available in the
* views.
* @constructor Creates a view helper. This function is called internally. You should never call it.
* @param {Object} data The data passed to the view. Helpers have access to it through this._data
*/
EJS.Helpers = function( data, extras ) {
this._data = data;
this._extras = extras;
extend(this, extras);
};
/* @prototype*/
EJS.Helpers.prototype = {
/**
* Hooks up a jQuery plugin on.
* @param {String} name the plugin name
*/
plugin: function( name ) {
var args = $.makeArray(arguments),
widget = args.shift();
return function( el ) {
var jq = $(el);
jq[widget].apply(jq, args);
};
},
/**
* Renders a partial view. This is deprecated in favor of <code>$.View()</code>.
*/
view: function( url, data, helpers ) {
helpers = helpers || this._extras;
data = data || this._data;
return $.View(url, data, helpers); //new EJS(options).render(data, helpers);
}
};
$.View.register({
suffix: "ejs",
//returns a function that renders the view
script: function( id, src ) {
return "jQuery.EJS(function(_CONTEXT,_VIEW) { " + new EJS({
text: src
}).template.out + " })";
},
renderer: function( id, text ) {
var ejs = new EJS({
text: text,
name: id
});
return function( data, helpers ) {
return ejs.render.call(ejs, data, helpers);
};
}
});
});