author | Dan |
Sun, 04 Jan 2009 00:55:40 -0500 | |
changeset 801 | eb8b23f11744 |
parent 582 | a38876c0793c |
child 832 | 7152ca0a0ce9 |
permissions | -rw-r--r-- |
1 | 1 |
// An implementation of Enano's template compiler in Javascript. Same exact API |
2 |
// as the PHP version - constructor accepts text, then the assign_vars, assign_bool, and run methods. |
|
3 |
||
582
a38876c0793c
Majorly reworked Javascript runtime stuff to use on-demand loading.
Dan
parents:
212
diff
changeset
|
4 |
window.templateParser = function(text) |
1 | 5 |
{ |
6 |
this.tpl_code = text; |
|
7 |
this.tpl_strings = new Object(); |
|
8 |
this.tpl_bool = new Object(); |
|
9 |
this.assign_vars = __tpAssignVars; |
|
10 |
this.assign_bool = __tpAssignBool; |
|
11 |
this.run = __tpRun; |
|
12 |
} |
|
13 |
||
582
a38876c0793c
Majorly reworked Javascript runtime stuff to use on-demand loading.
Dan
parents:
212
diff
changeset
|
14 |
window.__tpAssignVars = function(vars) |
1 | 15 |
{ |
16 |
for(var i in vars) |
|
17 |
{ |
|
18 |
this.tpl_strings[i] = vars[i]; |
|
19 |
} |
|
20 |
} |
|
21 |
||
582
a38876c0793c
Majorly reworked Javascript runtime stuff to use on-demand loading.
Dan
parents:
212
diff
changeset
|
22 |
window.__tpAssignBool = function(vars) |
1 | 23 |
{ |
24 |
for(var i in vars) |
|
25 |
{ |
|
26 |
this.tpl_bool[i] = ( vars[i] ) ? true : false; |
|
27 |
} |
|
28 |
} |
|
29 |
||
582
a38876c0793c
Majorly reworked Javascript runtime stuff to use on-demand loading.
Dan
parents:
212
diff
changeset
|
30 |
window.__tpRun = function() |
1 | 31 |
{ |
32 |
if(typeof(this.tpl_code) == 'string') |
|
33 |
{ |
|
34 |
tpl_code = __tpCompileTemplate(this.tpl_code); |
|
35 |
try { |
|
36 |
compiled = eval(tpl_code); |
|
37 |
} |
|
38 |
catch(e) |
|
39 |
{ |
|
40 |
alert(e); |
|
41 |
aclDebug(tpl_code); |
|
42 |
} |
|
43 |
return compiled; |
|
44 |
} |
|
45 |
return false; |
|
46 |
} |
|
47 |
||
582
a38876c0793c
Majorly reworked Javascript runtime stuff to use on-demand loading.
Dan
parents:
212
diff
changeset
|
48 |
window.__tpCompileTemplate = function(code) |
1 | 49 |
{ |
50 |
// Compile plaintext/template code to javascript code |
|
51 |
code = code.replace(/\\/g, "\\\\"); |
|
52 |
code = code.replace(/\'/g, "\\'"); |
|
53 |
code = code.replace(/\"/g, '\\"'); |
|
54 |
code = code.replace(new RegExp(unescape('%0A'), 'g'), '\\n'); |
|
55 |
code = "'" + code + "'"; |
|
56 |
code = code.replace(/\{([A-z0-9_-]+)\}/ig, "' + this.tpl_strings['$1'] + '"); |
|
212
30b857a6b811
Reworked comment system to not use HACKISH FIXES; AJAX comment framework is completely localized now
Dan
parents:
1
diff
changeset
|
57 |
code = code.replace(/\{lang:([a-z0-9_]+)\}/g, "' + $lang.get('$1') + '"); |
1 | 58 |
code = code.replace(/\<!-- BEGIN ([A-z0-9_-]+) --\>([\s\S]*?)\<!-- BEGINELSE \1 --\>([\s\S]*?)\<!-- END \1 --\>/ig, "' + ( ( this.tpl_bool['$1'] == true ) ? '$2' : '$3' ) + '"); |
59 |
code = code.replace(/\<!-- BEGIN ([A-z0-9_-]+) --\>([\s\S]*?)\<!-- END \1 --\>/ig, "' + ( ( this.tpl_bool['$1'] == true ) ? '$2' : '' ) + '"); |
|
60 |
return code; |
|
61 |
} |
|
62 |
||
582
a38876c0793c
Majorly reworked Javascript runtime stuff to use on-demand loading.
Dan
parents:
212
diff
changeset
|
63 |
window.__tpExtractVars = function(code) |
1 | 64 |
{ |
65 |
code = code.replace('\\', "\\\\"); |
|
66 |
code = code.replace("'", "\\'"); |
|
67 |
code = code.replace('"', '\\"'); |
|
68 |
code = code.replace(new RegExp(unescape('%0A'), 'g'), "\\n"); |
|
69 |
code = code.match(/\<!-- VAR ([A-z0-9_-]+) --\>([\s\S]*?)\<!-- ENDVAR \1 -->/g); |
|
70 |
code2 = ''; |
|
71 |
for(var i in code) |
|
72 |
if(typeof(code[i]) == 'string') |
|
73 |
code2 = code2 + code[i]; |
|
74 |
code = code2.replace(/\<!-- VAR ([A-z0-9_-]+) --\>([\s\S]*?)\<!-- ENDVAR \1 -->/g, "'$1' : \"$2\","); |
|
75 |
code = '( { ' + code + ' "________null________" : false } )'; |
|
76 |
vars = eval(code); |
|
77 |
return vars; |
|
78 |
} |
|
79 |