JSmarty

看到 Smarty 整套机制不错 所以想搬到 ASP 上来.
虽然以前也有类似的想法, 但没有这么系统的.

先来说下  Smarty 了吧.
它是 php 上的一套模板引擎, 它会对模板进行预编译, 进行缓存已提高速度.
具体介绍见 搜索...

DNT 其实用的就是类似的模板机制, 当然个人更喜欢 xsl 一点 所以我的 论坛(ACS)是用 xsl 来实现界面的

发现自己果然不喜欢写东西, 刚写了几个字就不想写了.
还是帖上有臭又硬的代码吧, 有兴趣的自己看.
目前只是基本原理的实现, 非常简陋, 也没有整理, 见谅.
预编译的模板本来想用一个很好的办法, 但无奈实在想不出既方便又高效的方法.
所以先用 eval 实现了. 个人总觉得这个东西比较邪乎 不大想用.
但小  rory 说 这个对性能影响不大, 具体我也没做性能测试, 不好说.

不知道把这个非完成品 粗糙的 并且不是 .NET 的放首页合适不.
如果不合适就请去掉.
另外对 Jscript 或者 xsl, 或者对这个项目有兴趣有意见的, 都可以来讨论.
还有本人脸皮薄, 代码写的不好, 请海涵, 勿攻击.

  1 None.gif //     JSmarty
  2 None.gif//     Version 1.0.0
  3 None.gif//     Engine
  4 None.gif
  5 None.gifString.prototype.StartWith  =   function  (params)
  6 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
  7InBlock.gif    return this.indexOf(params) == 0;
  8ExpandedBlockEnd.gif}

  9 None.gifString.prototype.EndWith  =   function  (params)
 10 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
 11InBlock.gif    return (this.length - this.lastIndexOf(params)) == params.length;
 12ExpandedBlockEnd.gif}

 13 None.gif
 14 None.gif var  Utility  =
 15 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
 16InBlock.gif    IsNullOrEmpty : function (text)
 17ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 18InBlock.gif        if (!isNaN(text))
 19ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 20InBlock.gif            return text === "";
 21ExpandedSubBlockEnd.gif        }

 22InBlock.gif        return true;
 23ExpandedSubBlockEnd.gif    }
,
 24InBlock.gif    IsArray : function (object)
 25ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 26InBlock.gif        return object instanceof Array;
 27ExpandedSubBlockEnd.gif    }

 28ExpandedBlockEnd.gif}

 29 None.gif
 30 None.gif var  SmartyConfig  =
 31 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
 32InBlock.gif    template_dir : "templates/",
 33InBlock.gif    compile_dir : "templates_c/",
 34InBlock.gif    config_dir : "configs/",
 35InBlock.gif    cache_dir : "cache/",
 36InBlock.gif    cache_prefix : "JSmarty::",
 37InBlock.gif    left_delimiter : "{/",
 38InBlock.gif    right_delimiter : "/}",
 39InBlock.gif    debug : false
 40ExpandedBlockEnd.gif}

 41 None.gif
 42 None.gif function  Smarty()
 43 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
 44InBlock.gif
 45InBlock.gif
 46InBlock.gif    
 47InBlock.gif    this.Version = "1.0.0";
 48InBlock.gif
 49InBlock.gif    var base = this;
 50InBlock.gif    var _tpl_vars = new Array();
 51InBlock.gif
 52InBlock.gif    this.assign = function (tpl_var, value)
 53ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 54InBlock.gif        if (Utility.IsArray(tpl_var))
 55ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 56InBlock.gif            for (name in tpl_var)
 57ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 58InBlock.gif                if (name != "")
 59InBlock.gif                    _tpl_vars[name] = tpl_var[name];
 60ExpandedSubBlockEnd.gif            }

 61ExpandedSubBlockEnd.gif        }

 62InBlock.gif        else
 63ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 64InBlock.gif            _tpl_vars[tpl_var] = value;
 65ExpandedSubBlockEnd.gif        }

 66ExpandedSubBlockEnd.gif    }

 67InBlock.gif    
 68InBlock.gif    this.assign_by_ref = function (tpl_var, value)
 69ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 70InBlock.gif        if (tpl_var != "")
 71InBlock.gif        _tpl_vars[tpl_var] = value;
 72ExpandedSubBlockEnd.gif    }

 73InBlock.gif
 74InBlock.gif    this.append = function (tpl_var, value, merge)
 75ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 76ExpandedSubBlockEnd.gif    }

 77InBlock.gif
 78InBlock.gif    this.append_by_ref = function (tpl_var, value, merge)
 79ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 80ExpandedSubBlockEnd.gif    }

 81InBlock.gif
 82InBlock.gif    this.clear_assign = function ()
 83ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 84InBlock.gif        delete _tpl_vars;
 85ExpandedSubBlockEnd.gif    }

 86InBlock.gif    this.getVar = function (name)
 87ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 88InBlock.gif        return _tpl_vars[name]
 89ExpandedSubBlockEnd.gif    }

 90InBlock.gif
 91InBlock.gif    this.display = function (resource_name, cache_id, compile_id)
 92ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 93InBlock.gif        var content = this.systemCache(resource_name);
 94InBlock.gif        if (SmartyConfig.debug || cache_id)
 95ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 96InBlock.gif            content = content.replace(/\</g, "&lt;");
 97InBlock.gif            content = content.replace(/\>/g, "&gt;");
 98InBlock.gif            content = content.replace(/\r\n/g, "<br />\r\n");
 99InBlock.gif            Response.Write(content);
100ExpandedSubBlockEnd.gif        }

101InBlock.gif        else
102ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
103InBlock.gif            eval(content);
104ExpandedSubBlockEnd.gif        }

105InBlock.gif        //this.fetch(resource_name, cache_id, compile_id, true);
106ExpandedSubBlockEnd.gif    }

107InBlock.gif    this.systemCache = function (resource_name)
108ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
109InBlock.gif
110InBlock.gif        var sc = new SmartyCache(resource_name);
111InBlock.gif        if (!sc.Exists())
112ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
113InBlock.gif            sc.Add(this.fileCache(resource_name));
114ExpandedSubBlockEnd.gif        }

115InBlock.gif        return sc.Get();
116ExpandedSubBlockEnd.gif    }

117InBlock.gif
118InBlock.gif    this.fileCache = function (resource_name)
119ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
120InBlock.gif        if (!this.IsExists(resource_name +".asp"))
121ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
122InBlock.gif            var sc = new SmartyCompiler();
123InBlock.gif            var content = sc.Load(resource_name);
124InBlock.gif            sc.SaveFile(resource_name, content);
125ExpandedSubBlockEnd.gif        }

126InBlock.gif        else
127ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
128InBlock.gif            var sc = new SmartyCompiler();
129InBlock.gif            content = sc.LoadFile(SmartyConfig.compile_dir + resource_name +".asp");
130ExpandedSubBlockEnd.gif        }

131InBlock.gif        return content;
132ExpandedSubBlockEnd.gif    }

133InBlock.gif
134InBlock.gif
135InBlock.gif    this.fetch = function (resource_name, cache_id, compile_id, display)
136ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
137InBlock.gif        var _query_string = Request.QueryString.item;
138ExpandedSubBlockEnd.gif    }

139InBlock.gif    this.IsExists = function (name)
140ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
141InBlock.gif        var file = Server.CreateObject("Scripting.FileSystemObject");
142InBlock.gif
143InBlock.gif        var result = file.FileExists(Server.MapPath(SmartyConfig.compile_dir + name));
144InBlock.gif        file = null;
145InBlock.gif
146InBlock.gif        return result;
147InBlock.gif
148ExpandedSubBlockEnd.gif    }

149InBlock.gif    this.RemoveAll = function ()
150ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
151InBlock.gif        var sc = new SmartyCache();
152InBlock.gif        sc.RemoveAll();
153ExpandedSubBlockEnd.gif    }

154InBlock.gif
155ExpandedBlockEnd.gif}

156 None.gif
157 None.gif function  SmartyCache()
158 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
159InBlock.gif    this.Key;
160InBlock.gif    this.Prefix = SmartyConfig.cache_prefix;
161InBlock.gif    var base = this;
162InBlock.gif
163InBlock.gif    NewKey = function (key)
164ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
165InBlock.gif        base.Key = base.Prefix + key;
166ExpandedSubBlockEnd.gif    }

167InBlock.gif    this.Exists = function (key)
168ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
169InBlock.gif        if (arguments.length == 1)
170ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
171InBlock.gif            NewKey(key);
172ExpandedSubBlockEnd.gif        }

173InBlock.gif        
174InBlock.gif        return !Utility.IsNullOrEmpty(Application.Contents(this.Key));
175ExpandedSubBlockEnd.gif    }

176InBlock.gif    this.Add = function (key, value)
177ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
178InBlock.gif        if (arguments.length == 2)
179ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
180InBlock.gif            NewKey(key);
181InBlock.gif            Application.Contents(this.Key) = value;
182ExpandedSubBlockEnd.gif        }

183InBlock.gif        else
184ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
185InBlock.gif            Application.Contents(this.Key) = key;
186ExpandedSubBlockEnd.gif        }

187InBlock.gif        
188ExpandedSubBlockEnd.gif    }

189InBlock.gif    this.Get = function (key)
190ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
191InBlock.gif        if (arguments.length == 1)
192ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
193InBlock.gif            NewKey(key);
194InBlock.gif            
195ExpandedSubBlockEnd.gif        }

196InBlock.gif        return Application.Contents(this.Key);
197ExpandedSubBlockEnd.gif    }

198InBlock.gif
199InBlock.gif    this.RemoveAll = function ()
200ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
201InBlock.gif        var e = new Enumerator(Application.Contents); 
202InBlock.gif        for (;!e.atEnd();e.moveNext())
203ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
204InBlock.gif            var key = e.item();
205InBlock.gif            if (key.StartWith(this.Prefix))
206ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
207InBlock.gif                Application.Contents.Remove(key);
208ExpandedSubBlockEnd.gif            }

209ExpandedSubBlockEnd.gif        }

210ExpandedSubBlockEnd.gif    }

211InBlock.gif    
212InBlock.gif    this.ctor = function(arguments)
213ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
214InBlock.gif        if (arguments.length == 1)
215ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
216InBlock.gif            NewKey(arguments[0])
217ExpandedSubBlockEnd.gif        }

218ExpandedSubBlockEnd.gif    }

219InBlock.gif
220InBlock.gif    this.ctor(arguments);
221InBlock.gif
222InBlock.gif    
223ExpandedBlockEnd.gif}

224 None.gif
225 None.gif
226 None.gif
227 None.gif function  SmartyCompiler()
228 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
229InBlock.gif
230InBlock.gif    var base = this;
231InBlock.gif
232InBlock.gif
233InBlock.gif    this.Load = function (tplName)
234ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
235InBlock.gif        return this.Tags(this.Generator(tplName));
236InBlock.gif
237ExpandedSubBlockEnd.gif    }

238InBlock.gif    this.Split = function (content)
239ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
240InBlock.gif        
241ExpandedSubBlockEnd.gif    }

242InBlock.gif
243InBlock.gif    this.Generator = function (tplName)
244ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
245InBlock.gif        var content = this.LoadFile(SmartyConfig.template_dir + tplName);
246InBlock.gif        //content = content.replace(/<!--#include tpl=\"(.+?)\"-->/ig, function ($0, $1) { return base.Generator($1); });
247InBlock.gif        content = this.Include(content);
248InBlock.gif        return content;
249ExpandedSubBlockEnd.gif    }

250InBlock.gif    this.Include = function (content)
251ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
252ExpandedSubBlockStart.gifContractedSubBlock.gif        return content.replace(/\dot.gif{include file=\"(.+?)\"\}/ig, function ($0, $1dot.gifreturn base.Generator($1); });
253ExpandedSubBlockEnd.gif    }

254InBlock.gif    this.Tags = function (content)
255ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
256InBlock.gif        
257InBlock.gif        content = content.replace(/\\/g, "\\\\");
258InBlock.gif        content = content.replace(/\"/g, "\\\"");
259InBlock.gif        content = content.replace(/(.*)\r\n?/g, "str.push(\"$1\\r\\n\");\r\n");
260ExpandedSubBlockStart.gifContractedSubBlock.gif        content = content.replace( /\dot.gif{\$(.+?)\}/g, "\"+ this.getVar(\"$1\"+\"");
261InBlock.gif        //content = content.replace(/str\.push\(\"\"\);\r\n/g, "");
262InBlock.gif        content = content.replace(/str\.push\(\"\"\+(.*)?\+\"\\r\\n\"\);\r\n/g, "str.push($1 \+\"\\r\\n\");\r\n");
263InBlock.gif        content = "var str = new Array();\r\n" + content;
264InBlock.gif        content += "Response.Write(str.join(\"\"));\r\n";
265InBlock.gif
266InBlock.gif        return content;
267InBlock.gif
268InBlock.gif        //var re = /\{\$(.+?)\}/ig;
269InBlock.gif        //return content.replace(re, "<%$1%\>");
270ExpandedSubBlockEnd.gif    }

271InBlock.gif    this.LoadFile = function (path)
272ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
273InBlock.gif        var file = Server.CreateObject("ADODB.Stream");
274InBlock.gif        var result;
275InBlock.gif        file.Charset = "utf-8";
276InBlock.gif        file.Open();
277InBlock.gif        //Response.Write(SmartyConfig.template_dir + fileName);
278InBlock.gif        //Response.Write("<br>");
279InBlock.gif        file.LoadFromFile(Server.MapPath(path));
280InBlock.gif        result = file.ReadText;
281ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//*
282InBlock.gif        try
283InBlock.gif        {
284InBlock.gif            file.LoadFromFile(Server.MapPath(SmartyConfig.template_dir + fileName));
285InBlock.gif            result = file.ReadText;
286InBlock.gif        }
287InBlock.gif        catch (e)
288InBlock.gif        {
289InBlock.gif            result = "";
290InBlock.gif        }
291ExpandedSubBlockEnd.gif        */

292InBlock.gif        file.Close();
293InBlock.gif        file = null;
294InBlock.gif
295InBlock.gif        return result;
296ExpandedSubBlockEnd.gif    }

297InBlock.gif    this.SaveFile = function (name, content)
298ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
299InBlock.gif        var file = Server.CreateObject("ADODB.Stream");
300InBlock.gif        file.Type = 2;
301InBlock.gif        file.Charset = "utf-8";
302InBlock.gif        file.Open();
303InBlock.gif        file.WriteText = content;
304InBlock.gif        file.SaveToFile(Server.Mappath(SmartyConfig.compile_dir + name +".asp"), 2);
305InBlock.gif        file.Close();
306InBlock.gif        file = null;
307ExpandedSubBlockEnd.gif    }

308InBlock.gif
309InBlock.gif    this.StripFileInfo = function ()
310ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
311ExpandedSubBlockEnd.gif    }

312InBlock.gif    this.AddFileInfo = function (content)
313ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
314InBlock.gif        
315ExpandedSubBlockEnd.gif    }

316InBlock.gif
317InBlock.gif
318ExpandedBlockEnd.gif}

转载于:https://www.cnblogs.com/slightboy/archive/2007/04/04/JSmarty.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值