This repository was archived by the owner on Mar 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathtmpl-node.js
More file actions
82 lines (66 loc) · 1.88 KB
/
Copy pathtmpl-node.js
File metadata and controls
82 lines (66 loc) · 1.88 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
// tmpl-node: a template module for node.js
// Jed Schmidt -
//
// inspired by John Resig's micro templates
// http://ejohn.org/blog/javascript-micro-templating/
var fs = require( "fs" ),
http = require( "http" ),
concat = Array.prototype.concat,
slice = Array.prototype.slice,
sys = require('sys');
exports.compile = compile;
exports.load = load;
exports.defaultContexts = [];
http.ServerResponse.prototype.render = function( name, c1, c2, etc ) {
var args = Array.prototype.slice.call( arguments ),
template = exports[ args.shift() ];
this.sendBody( template.apply( null, args ) );
this.finish();
};
function compile( str, name ) {
var fn = new Function( "o",
"var ret=[];with(o){ret.push('" +
str.replace(/[\r\t\n]/g, " ")
.replace(/'(?=[^%]*%>)/g,"\t")
.split("'").join("\\'")
.split("\t").join("'")
.replace(/<%=(.+?)%>/g, "',$1,'")
.split("<%").join("');")
.split("%>").join("ret.push('")
+ "');}return ret.join('');"
);
function ret() {
var args = concat.apply( exports.defaultContexts, arguments ),
x = -1,
i = args.length,
context = {},
name,
cur;
if ( i < 2 )
return fn( args[0] || context );
while ( x<i )
for ( name in ( cur = args[ ++x ] ) )
context[ name ] = cur[ name ];
return fn( context );
};
if ( typeof name === "string" )
exports[ name ] = ret;
return ret;
};
function load( dir, call ) {
var foo = {};
dir = dir.split('');
( dir[dir.length-1] === '/' )||( dir.push('/') );
dir = dir.join('');
fs.readdir(dir,function(err,dat){
var x;
(function walker(){
x = dat.pop();
fs.readFile(dir + x, function(e,data){
exports[x] = compile(data.toString());
( ( dat.length ) && ( walker() ) ) || ( call(exports) );
});
return true;
}());
});
};