author | Dan |
Fri, 30 Jul 2010 15:01:06 -0400 | |
changeset 1276 | dac5fad7ad6a |
parent 1269 | 35986c6b2150 |
child 1280 | 871f17a0d27d |
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 |
{ |
1227
bdac73ed481e
Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents:
832
diff
changeset
|
6 |
this.tpl_code = text; |
bdac73ed481e
Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents:
832
diff
changeset
|
7 |
this.tpl_strings = new Object(); |
bdac73ed481e
Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents:
832
diff
changeset
|
8 |
this.tpl_bool = new Object(); |
bdac73ed481e
Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents:
832
diff
changeset
|
9 |
this.assign_vars = __tpAssignVars; |
bdac73ed481e
Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents:
832
diff
changeset
|
10 |
this.assign_bool = __tpAssignBool; |
1269
35986c6b2150
Added template hooks in the JS template compiler. Attach to thook_<template hook name>. Use Echo() to echo HTML; access compiler instance with Template.tpl_{strings,bool}, etc.
Dan
parents:
1227
diff
changeset
|
11 |
this.fetch_hook = __tpFetchHook; |
1227
bdac73ed481e
Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents:
832
diff
changeset
|
12 |
this.run = __tpRun; |
1 | 13 |
} |
14 |
||
582
a38876c0793c
Majorly reworked Javascript runtime stuff to use on-demand loading.
Dan
parents:
212
diff
changeset
|
15 |
window.__tpAssignVars = function(vars) |
1 | 16 |
{ |
1227
bdac73ed481e
Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents:
832
diff
changeset
|
17 |
for(var i in vars) |
bdac73ed481e
Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents:
832
diff
changeset
|
18 |
{ |
bdac73ed481e
Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents:
832
diff
changeset
|
19 |
this.tpl_strings[i] = vars[i]; |
bdac73ed481e
Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents:
832
diff
changeset
|
20 |
} |
1 | 21 |
} |
22 |
||
582
a38876c0793c
Majorly reworked Javascript runtime stuff to use on-demand loading.
Dan
parents:
212
diff
changeset
|
23 |
window.__tpAssignBool = function(vars) |
1 | 24 |
{ |
1227
bdac73ed481e
Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents:
832
diff
changeset
|
25 |
for(var i in vars) |
bdac73ed481e
Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents:
832
diff
changeset
|
26 |
{ |
bdac73ed481e
Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents:
832
diff
changeset
|
27 |
this.tpl_bool[i] = ( vars[i] ) ? true : false; |
bdac73ed481e
Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents:
832
diff
changeset
|
28 |
} |
1 | 29 |
} |
30 |
||
582
a38876c0793c
Majorly reworked Javascript runtime stuff to use on-demand loading.
Dan
parents:
212
diff
changeset
|
31 |
window.__tpRun = function() |
1 | 32 |
{ |
1227
bdac73ed481e
Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents:
832
diff
changeset
|
33 |
if(typeof(this.tpl_code) == 'string') |
bdac73ed481e
Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents:
832
diff
changeset
|
34 |
{ |
bdac73ed481e
Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents:
832
diff
changeset
|
35 |
tpl_code = __tpCompileTemplate(this.tpl_code); |
bdac73ed481e
Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents:
832
diff
changeset
|
36 |
try { |
bdac73ed481e
Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents:
832
diff
changeset
|
37 |
compiled = eval(tpl_code); |
bdac73ed481e
Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents:
832
diff
changeset
|
38 |
} |
bdac73ed481e
Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents:
832
diff
changeset
|
39 |
catch(e) |
bdac73ed481e
Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents:
832
diff
changeset
|
40 |
{ |
bdac73ed481e
Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents:
832
diff
changeset
|
41 |
alert(e); |
bdac73ed481e
Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents:
832
diff
changeset
|
42 |
aclDebug(tpl_code); |
bdac73ed481e
Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents:
832
diff
changeset
|
43 |
} |
bdac73ed481e
Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents:
832
diff
changeset
|
44 |
return compiled; |
bdac73ed481e
Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents:
832
diff
changeset
|
45 |
} |
bdac73ed481e
Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents:
832
diff
changeset
|
46 |
return false; |
1 | 47 |
} |
48 |
||
582
a38876c0793c
Majorly reworked Javascript runtime stuff to use on-demand loading.
Dan
parents:
212
diff
changeset
|
49 |
window.__tpCompileTemplate = function(code) |
1 | 50 |
{ |
1227
bdac73ed481e
Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents:
832
diff
changeset
|
51 |
// Compile plaintext/template code to javascript code |
bdac73ed481e
Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents:
832
diff
changeset
|
52 |
code = code.replace(/\\/g, "\\\\"); |
bdac73ed481e
Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents:
832
diff
changeset
|
53 |
code = code.replace(/\'/g, "\\'"); |
bdac73ed481e
Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents:
832
diff
changeset
|
54 |
code = code.replace(/\"/g, '\\"'); |
bdac73ed481e
Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents:
832
diff
changeset
|
55 |
code = code.replace(new RegExp(unescape('%0A'), 'g'), '\\n'); |
bdac73ed481e
Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents:
832
diff
changeset
|
56 |
code = "'" + code + "'"; |
bdac73ed481e
Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents:
832
diff
changeset
|
57 |
code = code.replace(/\{([A-z0-9_-]+)\}/ig, "' + this.tpl_strings['$1'] + '"); |
bdac73ed481e
Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents:
832
diff
changeset
|
58 |
code = code.replace(/\{lang:([a-z0-9_]+)\}/g, "' + $lang.get('$1') + '"); |
bdac73ed481e
Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents:
832
diff
changeset
|
59 |
code = code.replace(/\<!-- IFSET ([A-z0-9_-]+) --\>([\s\S]*?)\<!-- BEGINELSE \1 --\>([\s\S]*?)\<!-- END \1 --\>/ig, "' + ( ( typeof(this.tpl_strings['$1']) == 'string' ) ? '$2' : '$3' ) + '"); |
bdac73ed481e
Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents:
832
diff
changeset
|
60 |
code = code.replace(/\<!-- IFSET ([A-z0-9_-]+) --\>([\s\S]*?)\<!-- END \1 --\>/ig, "' + ( ( typeof(this.tpl_strings['$1']) == 'string' ) ? '$2' : '' ) + '"); |
bdac73ed481e
Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents:
832
diff
changeset
|
61 |
code = code.replace(/\<!-- BEGIN ([A-z0-9_-]+) --\>([\s\S]*?)\<!-- BEGINELSE \1 --\>([\s\S]*?)\<!-- END \1 --\>/ig, "' + ( ( this.tpl_bool['$1'] == true ) ? '$2' : '$3' ) + '"); |
bdac73ed481e
Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents:
832
diff
changeset
|
62 |
code = code.replace(/\<!-- BEGIN ([A-z0-9_-]+) --\>([\s\S]*?)\<!-- END \1 --\>/ig, "' + ( ( this.tpl_bool['$1'] == true ) ? '$2' : '' ) + '"); |
1269
35986c6b2150
Added template hooks in the JS template compiler. Attach to thook_<template hook name>. Use Echo() to echo HTML; access compiler instance with Template.tpl_{strings,bool}, etc.
Dan
parents:
1227
diff
changeset
|
63 |
code = code.replace(/\<!-- HOOK ([A-z0-9_-]+) --\>/ig, "' + this.fetch_hook('$1') + '"); |
1227
bdac73ed481e
Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents:
832
diff
changeset
|
64 |
return code; |
1 | 65 |
} |
66 |
||
582
a38876c0793c
Majorly reworked Javascript runtime stuff to use on-demand loading.
Dan
parents:
212
diff
changeset
|
67 |
window.__tpExtractVars = function(code) |
1 | 68 |
{ |
1227
bdac73ed481e
Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents:
832
diff
changeset
|
69 |
code = code.replace('\\', "\\\\"); |
bdac73ed481e
Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents:
832
diff
changeset
|
70 |
code = code.replace("'", "\\'"); |
bdac73ed481e
Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents:
832
diff
changeset
|
71 |
code = code.replace('"', '\\"'); |
bdac73ed481e
Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents:
832
diff
changeset
|
72 |
code = code.replace(new RegExp(unescape('%0A'), 'g'), "\\n"); |
bdac73ed481e
Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents:
832
diff
changeset
|
73 |
code = code.match(/\<!-- VAR ([A-z0-9_-]+) --\>([\s\S]*?)\<!-- ENDVAR \1 -->/g); |
bdac73ed481e
Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents:
832
diff
changeset
|
74 |
code2 = ''; |
bdac73ed481e
Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents:
832
diff
changeset
|
75 |
for(var i in code) |
bdac73ed481e
Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents:
832
diff
changeset
|
76 |
if(typeof(code[i]) == 'string') |
bdac73ed481e
Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents:
832
diff
changeset
|
77 |
code2 = code2 + code[i]; |
bdac73ed481e
Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents:
832
diff
changeset
|
78 |
code = code2.replace(/\<!-- VAR ([A-z0-9_-]+) --\>([\s\S]*?)\<!-- ENDVAR \1 -->/g, "'$1' : \"$2\","); |
bdac73ed481e
Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents:
832
diff
changeset
|
79 |
code = '( { ' + code + ' "________null________" : false } )'; |
bdac73ed481e
Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents:
832
diff
changeset
|
80 |
vars = eval(code); |
bdac73ed481e
Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents:
832
diff
changeset
|
81 |
return vars; |
1 | 82 |
} |
83 |
||
1269
35986c6b2150
Added template hooks in the JS template compiler. Attach to thook_<template hook name>. Use Echo() to echo HTML; access compiler instance with Template.tpl_{strings,bool}, etc.
Dan
parents:
1227
diff
changeset
|
84 |
window.__tpFetchHook = function(hookid) |
35986c6b2150
Added template hooks in the JS template compiler. Attach to thook_<template hook name>. Use Echo() to echo HTML; access compiler instance with Template.tpl_{strings,bool}, etc.
Dan
parents:
1227
diff
changeset
|
85 |
{ |
35986c6b2150
Added template hooks in the JS template compiler. Attach to thook_<template hook name>. Use Echo() to echo HTML; access compiler instance with Template.tpl_{strings,bool}, etc.
Dan
parents:
1227
diff
changeset
|
86 |
var _ob = ''; |
35986c6b2150
Added template hooks in the JS template compiler. Attach to thook_<template hook name>. Use Echo() to echo HTML; access compiler instance with Template.tpl_{strings,bool}, etc.
Dan
parents:
1227
diff
changeset
|
87 |
window.Template = this; |
35986c6b2150
Added template hooks in the JS template compiler. Attach to thook_<template hook name>. Use Echo() to echo HTML; access compiler instance with Template.tpl_{strings,bool}, etc.
Dan
parents:
1227
diff
changeset
|
88 |
window.Echo = function(h) |
35986c6b2150
Added template hooks in the JS template compiler. Attach to thook_<template hook name>. Use Echo() to echo HTML; access compiler instance with Template.tpl_{strings,bool}, etc.
Dan
parents:
1227
diff
changeset
|
89 |
{ |
35986c6b2150
Added template hooks in the JS template compiler. Attach to thook_<template hook name>. Use Echo() to echo HTML; access compiler instance with Template.tpl_{strings,bool}, etc.
Dan
parents:
1227
diff
changeset
|
90 |
_ob += h; |
35986c6b2150
Added template hooks in the JS template compiler. Attach to thook_<template hook name>. Use Echo() to echo HTML; access compiler instance with Template.tpl_{strings,bool}, etc.
Dan
parents:
1227
diff
changeset
|
91 |
} |
35986c6b2150
Added template hooks in the JS template compiler. Attach to thook_<template hook name>. Use Echo() to echo HTML; access compiler instance with Template.tpl_{strings,bool}, etc.
Dan
parents:
1227
diff
changeset
|
92 |
eval(setHook('thook_' + hookid)); |
35986c6b2150
Added template hooks in the JS template compiler. Attach to thook_<template hook name>. Use Echo() to echo HTML; access compiler instance with Template.tpl_{strings,bool}, etc.
Dan
parents:
1227
diff
changeset
|
93 |
window.Echo = window.Template = false; |
35986c6b2150
Added template hooks in the JS template compiler. Attach to thook_<template hook name>. Use Echo() to echo HTML; access compiler instance with Template.tpl_{strings,bool}, etc.
Dan
parents:
1227
diff
changeset
|
94 |
return _ob; |
35986c6b2150
Added template hooks in the JS template compiler. Attach to thook_<template hook name>. Use Echo() to echo HTML; access compiler instance with Template.tpl_{strings,bool}, etc.
Dan
parents:
1227
diff
changeset
|
95 |
} |
35986c6b2150
Added template hooks in the JS template compiler. Attach to thook_<template hook name>. Use Echo() to echo HTML; access compiler instance with Template.tpl_{strings,bool}, etc.
Dan
parents:
1227
diff
changeset
|
96 |