虽然以前也有类似的想法, 但没有这么系统的.
先来说下 Smarty 了吧.
它是 php 上的一套模板引擎, 它会对模板进行预编译, 进行缓存已提高速度.
具体介绍见 搜索...
DNT 其实用的就是类似的模板机制, 当然个人更喜欢 xsl 一点 所以我的 论坛(ACS)是用 xsl 来实现界面的
发现自己果然不喜欢写东西, 刚写了几个字就不想写了.
还是帖上有臭又硬的代码吧, 有兴趣的自己看.
目前只是基本原理的实现, 非常简陋, 也没有整理, 见谅.
预编译的模板本来想用一个很好的办法, 但无奈实在想不出既方便又高效的方法.
所以先用 eval 实现了. 个人总觉得这个东西比较邪乎 不大想用.
但小 rory 说 这个对性能影响不大, 具体我也没做性能测试, 不好说.
不知道把这个非完成品 粗糙的 并且不是 .NET 的放首页合适不.
如果不合适就请去掉.
另外对 Jscript 或者 xsl, 或者对这个项目有兴趣有意见的, 都可以来讨论.
还有本人脸皮薄, 代码写的不好, 请海涵, 勿攻击.
1
//
JSmarty
2 // Version 1.0.0
3 // Engine
4
5 String.prototype.StartWith = function (params)
6 {
7 return this.indexOf(params) == 0;
8}
9 String.prototype.EndWith = function (params)
10 {
11 return (this.length - this.lastIndexOf(params)) == params.length;
12}
13
14 var Utility =
15 {
16 IsNullOrEmpty : function (text)
17 {
18 if (!isNaN(text))
19 {
20 return text === "";
21 }
22 return true;
23 },
24 IsArray : function (object)
25 {
26 return object instanceof Array;
27 }
28}
29
30 var SmartyConfig =
31 {
32 template_dir : "templates/",
33 compile_dir : "templates_c/",
34 config_dir : "configs/",
35 cache_dir : "cache/",
36 cache_prefix : "JSmarty::",
37 left_delimiter : "{/",
38 right_delimiter : "/}",
39 debug : false
40}
41
42 function Smarty()
43 {
44
45
46
47 this.Version = "1.0.0";
48
49 var base = this;
50 var _tpl_vars = new Array();
51
52 this.assign = function (tpl_var, value)
53 {
54 if (Utility.IsArray(tpl_var))
55 {
56 for (name in tpl_var)
57 {
58 if (name != "")
59 _tpl_vars[name] = tpl_var[name];
60 }
61 }
62 else
63 {
64 _tpl_vars[tpl_var] = value;
65 }
66 }
67
68 this.assign_by_ref = function (tpl_var, value)
69 {
70 if (tpl_var != "")
71 _tpl_vars[tpl_var] = value;
72 }
73
74 this.append = function (tpl_var, value, merge)
75 {
76 }
77
78 this.append_by_ref = function (tpl_var, value, merge)
79 {
80 }
81
82 this.clear_assign = function ()
83 {
84 delete _tpl_vars;
85 }
86 this.getVar = function (name)
87 {
88 return _tpl_vars[name]
89 }
90
91 this.display = function (resource_name, cache_id, compile_id)
92 {
93 var content = this.systemCache(resource_name);
94 if (SmartyConfig.debug || cache_id)
95 {
96 content = content.replace(/\</g, "<");
97 content = content.replace(/\>/g, ">");
98 content = content.replace(/\r\n/g, "<br />\r\n");
99 Response.Write(content);
100 }
101 else
102 {
103 eval(content);
104 }
105 //this.fetch(resource_name, cache_id, compile_id, true);
106 }
107 this.systemCache = function (resource_name)
108 {
109
110 var sc = new SmartyCache(resource_name);
111 if (!sc.Exists())
112 {
113 sc.Add(this.fileCache(resource_name));
114 }
115 return sc.Get();
116 }
117
118 this.fileCache = function (resource_name)
119 {
120 if (!this.IsExists(resource_name +".asp"))
121 {
122 var sc = new SmartyCompiler();
123 var content = sc.Load(resource_name);
124 sc.SaveFile(resource_name, content);
125 }
126 else
127 {
128 var sc = new SmartyCompiler();
129 content = sc.LoadFile(SmartyConfig.compile_dir + resource_name +".asp");
130 }
131 return content;
132 }
133
134
135 this.fetch = function (resource_name, cache_id, compile_id, display)
136 {
137 var _query_string = Request.QueryString.item;
138 }
139 this.IsExists = function (name)
140 {
141 var file = Server.CreateObject("Scripting.FileSystemObject");
142
143 var result = file.FileExists(Server.MapPath(SmartyConfig.compile_dir + name));
144 file = null;
145
146 return result;
147
148 }
149 this.RemoveAll = function ()
150 {
151 var sc = new SmartyCache();
152 sc.RemoveAll();
153 }
154
155}
156
157 function SmartyCache()
158 {
159 this.Key;
160 this.Prefix = SmartyConfig.cache_prefix;
161 var base = this;
162
163 NewKey = function (key)
164 {
165 base.Key = base.Prefix + key;
166 }
167 this.Exists = function (key)
168 {
169 if (arguments.length == 1)
170 {
171 NewKey(key);
172 }
173
174 return !Utility.IsNullOrEmpty(Application.Contents(this.Key));
175 }
176 this.Add = function (key, value)
177 {
178 if (arguments.length == 2)
179 {
180 NewKey(key);
181 Application.Contents(this.Key) = value;
182 }
183 else
184 {
185 Application.Contents(this.Key) = key;
186 }
187
188 }
189 this.Get = function (key)
190 {
191 if (arguments.length == 1)
192 {
193 NewKey(key);
194
195 }
196 return Application.Contents(this.Key);
197 }
198
199 this.RemoveAll = function ()
200 {
201 var e = new Enumerator(Application.Contents);
202 for (;!e.atEnd();e.moveNext())
203 {
204 var key = e.item();
205 if (key.StartWith(this.Prefix))
206 {
207 Application.Contents.Remove(key);
208 }
209 }
210 }
211
212 this.ctor = function(arguments)
213 {
214 if (arguments.length == 1)
215 {
216 NewKey(arguments[0])
217 }
218 }
219
220 this.ctor(arguments);
221
222
223}
224
225
226
227 function SmartyCompiler()
228 {
229
230 var base = this;
231
232
233 this.Load = function (tplName)
234 {
235 return this.Tags(this.Generator(tplName));
236
237 }
238 this.Split = function (content)
239 {
240
241 }
242
243 this.Generator = function (tplName)
244 {
245 var content = this.LoadFile(SmartyConfig.template_dir + tplName);
246 //content = content.replace(/<!--#include tpl=\"(.+?)\"-->/ig, function ($0, $1) { return base.Generator($1); });
247 content = this.Include(content);
248 return content;
249 }
250 this.Include = function (content)
251 {
252 return content.replace(/\{include file=\"(.+?)\"\}/ig, function ($0, $1) { return base.Generator($1); });
253 }
254 this.Tags = function (content)
255 {
256
257 content = content.replace(/\\/g, "\\\\");
258 content = content.replace(/\"/g, "\\\"");
259 content = content.replace(/(.*)\r\n?/g, "str.push(\"$1\\r\\n\");\r\n");
260 content = content.replace( /\{\$(.+?)\}/g, "\"+ this.getVar(\"$1\") +\"");
261 //content = content.replace(/str\.push\(\"\"\);\r\n/g, "");
262 content = content.replace(/str\.push\(\"\"\+(.*)?\+\"\\r\\n\"\);\r\n/g, "str.push($1 \+\"\\r\\n\");\r\n");
263 content = "var str = new Array();\r\n" + content;
264 content += "Response.Write(str.join(\"\"));\r\n";
265
266 return content;
267
268 //var re = /\{\$(.+?)\}/ig;
269 //return content.replace(re, "<%$1%\>");
270 }
271 this.LoadFile = function (path)
272 {
273 var file = Server.CreateObject("ADODB.Stream");
274 var result;
275 file.Charset = "utf-8";
276 file.Open();
277 //Response.Write(SmartyConfig.template_dir + fileName);
278 //Response.Write("<br>");
279 file.LoadFromFile(Server.MapPath(path));
280 result = file.ReadText;
281 /**//*
282 try
283 {
284 file.LoadFromFile(Server.MapPath(SmartyConfig.template_dir + fileName));
285 result = file.ReadText;
286 }
287 catch (e)
288 {
289 result = "";
290 }
291 */
292 file.Close();
293 file = null;
294
295 return result;
296 }
297 this.SaveFile = function (name, content)
298 {
299 var file = Server.CreateObject("ADODB.Stream");
300 file.Type = 2;
301 file.Charset = "utf-8";
302 file.Open();
303 file.WriteText = content;
304 file.SaveToFile(Server.Mappath(SmartyConfig.compile_dir + name +".asp"), 2);
305 file.Close();
306 file = null;
307 }
308
309 this.StripFileInfo = function ()
310 {
311 }
312 this.AddFileInfo = function (content)
313 {
314
315 }
316
317
318}
2 // Version 1.0.0
3 // Engine
4
5 String.prototype.StartWith = function (params)
6 {
7 return this.indexOf(params) == 0;
8}
9 String.prototype.EndWith = function (params)
10 {
11 return (this.length - this.lastIndexOf(params)) == params.length;
12}
13
14 var Utility =
15 {
16 IsNullOrEmpty : function (text)
17 {
18 if (!isNaN(text))
19 {
20 return text === "";
21 }
22 return true;
23 },
24 IsArray : function (object)
25 {
26 return object instanceof Array;
27 }
28}
29
30 var SmartyConfig =
31 {
32 template_dir : "templates/",
33 compile_dir : "templates_c/",
34 config_dir : "configs/",
35 cache_dir : "cache/",
36 cache_prefix : "JSmarty::",
37 left_delimiter : "{/",
38 right_delimiter : "/}",
39 debug : false
40}
41
42 function Smarty()
43 {
44
45
46
47 this.Version = "1.0.0";
48
49 var base = this;
50 var _tpl_vars = new Array();
51
52 this.assign = function (tpl_var, value)
53 {
54 if (Utility.IsArray(tpl_var))
55 {
56 for (name in tpl_var)
57 {
58 if (name != "")
59 _tpl_vars[name] = tpl_var[name];
60 }
61 }
62 else
63 {
64 _tpl_vars[tpl_var] = value;
65 }
66 }
67
68 this.assign_by_ref = function (tpl_var, value)
69 {
70 if (tpl_var != "")
71 _tpl_vars[tpl_var] = value;
72 }
73
74 this.append = function (tpl_var, value, merge)
75 {
76 }
77
78 this.append_by_ref = function (tpl_var, value, merge)
79 {
80 }
81
82 this.clear_assign = function ()
83 {
84 delete _tpl_vars;
85 }
86 this.getVar = function (name)
87 {
88 return _tpl_vars[name]
89 }
90
91 this.display = function (resource_name, cache_id, compile_id)
92 {
93 var content = this.systemCache(resource_name);
94 if (SmartyConfig.debug || cache_id)
95 {
96 content = content.replace(/\</g, "<");
97 content = content.replace(/\>/g, ">");
98 content = content.replace(/\r\n/g, "<br />\r\n");
99 Response.Write(content);
100 }
101 else
102 {
103 eval(content);
104 }
105 //this.fetch(resource_name, cache_id, compile_id, true);
106 }
107 this.systemCache = function (resource_name)
108 {
109
110 var sc = new SmartyCache(resource_name);
111 if (!sc.Exists())
112 {
113 sc.Add(this.fileCache(resource_name));
114 }
115 return sc.Get();
116 }
117
118 this.fileCache = function (resource_name)
119 {
120 if (!this.IsExists(resource_name +".asp"))
121 {
122 var sc = new SmartyCompiler();
123 var content = sc.Load(resource_name);
124 sc.SaveFile(resource_name, content);
125 }
126 else
127 {
128 var sc = new SmartyCompiler();
129 content = sc.LoadFile(SmartyConfig.compile_dir + resource_name +".asp");
130 }
131 return content;
132 }
133
134
135 this.fetch = function (resource_name, cache_id, compile_id, display)
136 {
137 var _query_string = Request.QueryString.item;
138 }
139 this.IsExists = function (name)
140 {
141 var file = Server.CreateObject("Scripting.FileSystemObject");
142
143 var result = file.FileExists(Server.MapPath(SmartyConfig.compile_dir + name));
144 file = null;
145
146 return result;
147
148 }
149 this.RemoveAll = function ()
150 {
151 var sc = new SmartyCache();
152 sc.RemoveAll();
153 }
154
155}
156
157 function SmartyCache()
158 {
159 this.Key;
160 this.Prefix = SmartyConfig.cache_prefix;
161 var base = this;
162
163 NewKey = function (key)
164 {
165 base.Key = base.Prefix + key;
166 }
167 this.Exists = function (key)
168 {
169 if (arguments.length == 1)
170 {
171 NewKey(key);
172 }
173
174 return !Utility.IsNullOrEmpty(Application.Contents(this.Key));
175 }
176 this.Add = function (key, value)
177 {
178 if (arguments.length == 2)
179 {
180 NewKey(key);
181 Application.Contents(this.Key) = value;
182 }
183 else
184 {
185 Application.Contents(this.Key) = key;
186 }
187
188 }
189 this.Get = function (key)
190 {
191 if (arguments.length == 1)
192 {
193 NewKey(key);
194
195 }
196 return Application.Contents(this.Key);
197 }
198
199 this.RemoveAll = function ()
200 {
201 var e = new Enumerator(Application.Contents);
202 for (;!e.atEnd();e.moveNext())
203 {
204 var key = e.item();
205 if (key.StartWith(this.Prefix))
206 {
207 Application.Contents.Remove(key);
208 }
209 }
210 }
211
212 this.ctor = function(arguments)
213 {
214 if (arguments.length == 1)
215 {
216 NewKey(arguments[0])
217 }
218 }
219
220 this.ctor(arguments);
221
222
223}
224
225
226
227 function SmartyCompiler()
228 {
229
230 var base = this;
231
232
233 this.Load = function (tplName)
234 {
235 return this.Tags(this.Generator(tplName));
236
237 }
238 this.Split = function (content)
239 {
240
241 }
242
243 this.Generator = function (tplName)
244 {
245 var content = this.LoadFile(SmartyConfig.template_dir + tplName);
246 //content = content.replace(/<!--#include tpl=\"(.+?)\"-->/ig, function ($0, $1) { return base.Generator($1); });
247 content = this.Include(content);
248 return content;
249 }
250 this.Include = function (content)
251 {
252 return content.replace(/\{include file=\"(.+?)\"\}/ig, function ($0, $1) { return base.Generator($1); });
253 }
254 this.Tags = function (content)
255 {
256
257 content = content.replace(/\\/g, "\\\\");
258 content = content.replace(/\"/g, "\\\"");
259 content = content.replace(/(.*)\r\n?/g, "str.push(\"$1\\r\\n\");\r\n");
260 content = content.replace( /\{\$(.+?)\}/g, "\"+ this.getVar(\"$1\") +\"");
261 //content = content.replace(/str\.push\(\"\"\);\r\n/g, "");
262 content = content.replace(/str\.push\(\"\"\+(.*)?\+\"\\r\\n\"\);\r\n/g, "str.push($1 \+\"\\r\\n\");\r\n");
263 content = "var str = new Array();\r\n" + content;
264 content += "Response.Write(str.join(\"\"));\r\n";
265
266 return content;
267
268 //var re = /\{\$(.+?)\}/ig;
269 //return content.replace(re, "<%$1%\>");
270 }
271 this.LoadFile = function (path)
272 {
273 var file = Server.CreateObject("ADODB.Stream");
274 var result;
275 file.Charset = "utf-8";
276 file.Open();
277 //Response.Write(SmartyConfig.template_dir + fileName);
278 //Response.Write("<br>");
279 file.LoadFromFile(Server.MapPath(path));
280 result = file.ReadText;
281 /**//*
282 try
283 {
284 file.LoadFromFile(Server.MapPath(SmartyConfig.template_dir + fileName));
285 result = file.ReadText;
286 }
287 catch (e)
288 {
289 result = "";
290 }
291 */
292 file.Close();
293 file = null;
294
295 return result;
296 }
297 this.SaveFile = function (name, content)
298 {
299 var file = Server.CreateObject("ADODB.Stream");
300 file.Type = 2;
301 file.Charset = "utf-8";
302 file.Open();
303 file.WriteText = content;
304 file.SaveToFile(Server.Mappath(SmartyConfig.compile_dir + name +".asp"), 2);
305 file.Close();
306 file = null;
307 }
308
309 this.StripFileInfo = function ()
310 {
311 }
312 this.AddFileInfo = function (content)
313 {
314
315 }
316
317
318}