[原创]GT-Template, 一个超轻量级的js模板工具.
GT-Template beta? ---- 作者 fins
这是一个简单小巧的 模板工具类, 用来帮助使用者快速的拼装出所需要的字符串.
简单说明:
模板中 #{ ... /} 之间的数据将被 作为js脚本执行, 而其中VAR关键字代表的就是 seed 的值(从外部传入的一个变化的值).
模板分成两部分来执行, compile 和 run.
#{..../}之间的部分在run期间被执行,
#{#...../} 之间的部分在编译期执行.编译期执行的代码中不能传入seed.
更多的说明也不用再写了,因为确实很简单的功能 呵呵.
原理:
实际上就是 利用传入的参数对 模板字符串中 #{ .../} 的部分进行替换.(对 #{ .../}之间的部分执行eval(),之后再替换).
其他:
以后也许会对这个模板做一些争强和扩充,那要看具体的需求了.
目前我主要就是用他批量的生成测试数据.
如 批量的 insert sql, 批量的 json 数据, 批量的 <tr><td>..</td>....</tr>
以前我写过一个 TrimPath Template(一个已经死去的"js模板组件"项目) 的增强版,所以对这方面还是有一定的研究和心得的.
希望大家能够多提需求,只要实用 常用,我会加进去的.
示例:字符串 inTmp : Hello,#{ VAR.custName /},My name is #{ VAR.myName /}.var templateC= GT.compileTemplate(inTmp);var inArg= {custName 'Tom',myName: 'fins'}var outResult = GT.runTemplate(templateC,inArg);字符串 outResult : Hello,Tom,My name is fins.在#{ .../} 也可以写如相对 复杂的 js脚本, 例如 #{ VAR.gender==1?'男':'女' /}如果再复杂些建议写成 function,然后在 #{ /}内部调用 (可以写成内联的function 也可以用调用时传参的).真的是超轻量级, 所以就不发附件了 直接贴上来if (!GT){GT={};}// template为 一个字符串, 返回的是编译好的一个字符串数组,该数组为 runTemplate:方法的输入参数之一.GT.compileTemplate= function(template){var TEMPLATE_START="#{";var TEMPLATE_END="/}";var templateC=[];var snippets=[];var current=0;while(true){var start= template.indexOf( TEMPLATE_START ,current);var sBegin=start+2;var sEnd=template.indexOf( TEMPLATE_END ,sBegin);if (sBegin>=2 && sEnd>sBegin){templateC.push(template.substring(current,start) );var sn=template.substring(sBegin,sEnd);if (sn.indexOf("#")==0){sn=eval( sn.substring(1) );}else{snippets.push(templateC.length);}templateC.push( sn );}else{templateC.push( template.substring(current) );break;}current=sEnd+2;}templateC.push(snippets);return templateC;};// templateC为编译好的字符串数组, invar为模板中要传入的变量.// 如果要传入多个变量, 请使用 {},或[] .GT.runTemplate= function(templateC,invar){var VAR=invar;var snippets=templateC[templateC.length-1];var rs=[];var sIdx=0;for (var i=0;i<templateC.length-1;i++ ){if (snippets[sIdx]==i){rs.push( eval(templateC[i]) );sIdx++;}else {rs.push( templateC[i] )}}return rs.join("");};更强大的jsTemplate语言 我暂时没有时间去写 不过我倒是很希望可以写一个更好更强大的 呵呵 如果能写个 js版的 freemarker我想一定是一件很有趣的事情 呵呵
更强大的jsTemplate语言 我暂时没有时间去写 不过我倒是很希望可以写一个更好更强大的 呵呵 如果能写个 js版的 freemarker我想一定是一件很有趣的事情 呵呵
我想知道你对jst有什么改造。 我觉得现在jst够用。the "macro" can be placed anywhere in the template.ex:{for vo in tdata}${testMacro(vo)}{/for}{macro testMacro(v)}....{/macro}change log:build 20060817:1 a new better parse() Method. shorter ,faster , clearer. 2 add a Debuger interface method. it can help developer to debug.3 fix a bug in emitSectionText() Method ;build 20060813:1 add some statement. (for foreach while switch inline ....)2 add some modifier (transform times ...)3 change block function ( cdata eval minify ) extract them from parse Method.4 change the statement-RegularExpressions and blockFunction-RegularExpressions .now they are built automatically.5 add TrimPath.tagMaxLength . in ver1.0.38 ,the maximum statement tag length is 10 and 10 is a hardcode.6 extract them:var self_OUT=" var _OUT_arr = []; var _OUT = { write: function(m) { if (m) _OUT_arr.push(m); } }; ";var self_OUTResultArr = "_OUT_arr.join('')" ;7 add a helper function TrimPath.replaceAll();the replace method in javascript is cann't replace all match substring except use RegularExpressions.the TrimPath.replaceAll(str,oldsubstring,newsubstring) don't use RegularExpressions. 8 add TrimPath.parseXMLTemplate.now you can save the template in the CDATA node of any XML File.9 etc..... 6 楼 Tin 2007-10-11 JST没有死呀。这个轻量的引擎我觉得还是太轻了,内容太少。
你说的“以前我写过一个 TrimPath Template(一个已经死去的"js模板组件"项目) 的增强版”,不知道你的扩展是否已经提交给JST项目了呢?我觉得JST是页面端MVC的最佳模板引擎。