1 // An implementation of Enano's template compiler in Javascript. Same exact API |
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. |
2 // as the PHP version - constructor accepts text, then the assign_vars, assign_bool, and run methods. |
3 |
3 |
4 window.templateParser = function(text) |
4 window.templateParser = function(text) |
5 { |
5 { |
6 this.tpl_code = text; |
6 this.tpl_code = text; |
7 this.tpl_strings = new Object(); |
7 this.tpl_strings = new Object(); |
8 this.tpl_bool = new Object(); |
8 this.tpl_bool = new Object(); |
9 this.assign_vars = __tpAssignVars; |
9 this.assign_vars = __tpAssignVars; |
10 this.assign_bool = __tpAssignBool; |
10 this.assign_bool = __tpAssignBool; |
11 this.run = __tpRun; |
11 this.run = __tpRun; |
12 } |
12 } |
13 |
13 |
14 window.__tpAssignVars = function(vars) |
14 window.__tpAssignVars = function(vars) |
15 { |
15 { |
16 for(var i in vars) |
16 for(var i in vars) |
17 { |
17 { |
18 this.tpl_strings[i] = vars[i]; |
18 this.tpl_strings[i] = vars[i]; |
19 } |
19 } |
20 } |
20 } |
21 |
21 |
22 window.__tpAssignBool = function(vars) |
22 window.__tpAssignBool = function(vars) |
23 { |
23 { |
24 for(var i in vars) |
24 for(var i in vars) |
25 { |
25 { |
26 this.tpl_bool[i] = ( vars[i] ) ? true : false; |
26 this.tpl_bool[i] = ( vars[i] ) ? true : false; |
27 } |
27 } |
28 } |
28 } |
29 |
29 |
30 window.__tpRun = function() |
30 window.__tpRun = function() |
31 { |
31 { |
32 if(typeof(this.tpl_code) == 'string') |
32 if(typeof(this.tpl_code) == 'string') |
33 { |
33 { |
34 tpl_code = __tpCompileTemplate(this.tpl_code); |
34 tpl_code = __tpCompileTemplate(this.tpl_code); |
35 try { |
35 try { |
36 compiled = eval(tpl_code); |
36 compiled = eval(tpl_code); |
37 } |
37 } |
38 catch(e) |
38 catch(e) |
39 { |
39 { |
40 alert(e); |
40 alert(e); |
41 aclDebug(tpl_code); |
41 aclDebug(tpl_code); |
42 } |
42 } |
43 return compiled; |
43 return compiled; |
44 } |
44 } |
45 return false; |
45 return false; |
46 } |
46 } |
47 |
47 |
48 window.__tpCompileTemplate = function(code) |
48 window.__tpCompileTemplate = function(code) |
49 { |
49 { |
50 // Compile plaintext/template code to javascript code |
50 // Compile plaintext/template code to javascript code |
51 code = code.replace(/\\/g, "\\\\"); |
51 code = code.replace(/\\/g, "\\\\"); |
52 code = code.replace(/\'/g, "\\'"); |
52 code = code.replace(/\'/g, "\\'"); |
53 code = code.replace(/\"/g, '\\"'); |
53 code = code.replace(/\"/g, '\\"'); |
54 code = code.replace(new RegExp(unescape('%0A'), 'g'), '\\n'); |
54 code = code.replace(new RegExp(unescape('%0A'), 'g'), '\\n'); |
55 code = "'" + code + "'"; |
55 code = "'" + code + "'"; |
56 code = code.replace(/\{([A-z0-9_-]+)\}/ig, "' + this.tpl_strings['$1'] + '"); |
56 code = code.replace(/\{([A-z0-9_-]+)\}/ig, "' + this.tpl_strings['$1'] + '"); |
57 code = code.replace(/\{lang:([a-z0-9_]+)\}/g, "' + $lang.get('$1') + '"); |
57 code = code.replace(/\{lang:([a-z0-9_]+)\}/g, "' + $lang.get('$1') + '"); |
58 code = code.replace(/\<!-- IFSET ([A-z0-9_-]+) --\>([\s\S]*?)\<!-- BEGINELSE \1 --\>([\s\S]*?)\<!-- END \1 --\>/ig, "' + ( ( typeof(this.tpl_strings['$1']) == 'string' ) ? '$2' : '$3' ) + '"); |
58 code = code.replace(/\<!-- IFSET ([A-z0-9_-]+) --\>([\s\S]*?)\<!-- BEGINELSE \1 --\>([\s\S]*?)\<!-- END \1 --\>/ig, "' + ( ( typeof(this.tpl_strings['$1']) == 'string' ) ? '$2' : '$3' ) + '"); |
59 code = code.replace(/\<!-- IFSET ([A-z0-9_-]+) --\>([\s\S]*?)\<!-- END \1 --\>/ig, "' + ( ( typeof(this.tpl_strings['$1']) == 'string' ) ? '$2' : '' ) + '"); |
59 code = code.replace(/\<!-- IFSET ([A-z0-9_-]+) --\>([\s\S]*?)\<!-- END \1 --\>/ig, "' + ( ( typeof(this.tpl_strings['$1']) == 'string' ) ? '$2' : '' ) + '"); |
60 code = code.replace(/\<!-- BEGIN ([A-z0-9_-]+) --\>([\s\S]*?)\<!-- BEGINELSE \1 --\>([\s\S]*?)\<!-- END \1 --\>/ig, "' + ( ( this.tpl_bool['$1'] == true ) ? '$2' : '$3' ) + '"); |
60 code = code.replace(/\<!-- BEGIN ([A-z0-9_-]+) --\>([\s\S]*?)\<!-- BEGINELSE \1 --\>([\s\S]*?)\<!-- END \1 --\>/ig, "' + ( ( this.tpl_bool['$1'] == true ) ? '$2' : '$3' ) + '"); |
61 code = code.replace(/\<!-- BEGIN ([A-z0-9_-]+) --\>([\s\S]*?)\<!-- END \1 --\>/ig, "' + ( ( this.tpl_bool['$1'] == true ) ? '$2' : '' ) + '"); |
61 code = code.replace(/\<!-- BEGIN ([A-z0-9_-]+) --\>([\s\S]*?)\<!-- END \1 --\>/ig, "' + ( ( this.tpl_bool['$1'] == true ) ? '$2' : '' ) + '"); |
62 return code; |
62 return code; |
63 } |
63 } |
64 |
64 |
65 window.__tpExtractVars = function(code) |
65 window.__tpExtractVars = function(code) |
66 { |
66 { |
67 code = code.replace('\\', "\\\\"); |
67 code = code.replace('\\', "\\\\"); |
68 code = code.replace("'", "\\'"); |
68 code = code.replace("'", "\\'"); |
69 code = code.replace('"', '\\"'); |
69 code = code.replace('"', '\\"'); |
70 code = code.replace(new RegExp(unescape('%0A'), 'g'), "\\n"); |
70 code = code.replace(new RegExp(unescape('%0A'), 'g'), "\\n"); |
71 code = code.match(/\<!-- VAR ([A-z0-9_-]+) --\>([\s\S]*?)\<!-- ENDVAR \1 -->/g); |
71 code = code.match(/\<!-- VAR ([A-z0-9_-]+) --\>([\s\S]*?)\<!-- ENDVAR \1 -->/g); |
72 code2 = ''; |
72 code2 = ''; |
73 for(var i in code) |
73 for(var i in code) |
74 if(typeof(code[i]) == 'string') |
74 if(typeof(code[i]) == 'string') |
75 code2 = code2 + code[i]; |
75 code2 = code2 + code[i]; |
76 code = code2.replace(/\<!-- VAR ([A-z0-9_-]+) --\>([\s\S]*?)\<!-- ENDVAR \1 -->/g, "'$1' : \"$2\","); |
76 code = code2.replace(/\<!-- VAR ([A-z0-9_-]+) --\>([\s\S]*?)\<!-- ENDVAR \1 -->/g, "'$1' : \"$2\","); |
77 code = '( { ' + code + ' "________null________" : false } )'; |
77 code = '( { ' + code + ' "________null________" : false } )'; |
78 vars = eval(code); |
78 vars = eval(code); |
79 return vars; |
79 return vars; |
80 } |
80 } |
81 |
81 |