我正在研究John Resig的微模板框架,它很棒,很小,符合我的要求。唯一的问题是语法混淆了ASP。净框架。这是因为
内的任何内容<%=id%>
<%=(i % 2 == 1 ? " even" : "")%>
表达式语法使用服务器变量求值。有没有人修改过代码来使用ASP.NET?
只需将解析函数中的<%
和%>
分别更改为<#
和#>
。我看过这样做,效果很好。
// Convert the template into pure JavaScript
str
.replace(/[rtn]/g, " ")
.split("<#").join("t")
.replace(/((^|#>)[^t]*)'/g, "$1r") //note the extra here
.replace(/t=(.*?)#>/g, "',$1,'") //and here
.split("t").join("');")
.split("#>").join("p.push('")
.split("r").join("\'")
…等。
如果因为<%
和%>
而不能工作,那么您可以轻松更改源代码。
str
.replace(/[rtn]/g, " ")
.split("<%").join("t") // this % you could change to @ or whatever
.replace(/((^|%>)[^t]*)'/g, "$1r") // same here
.replace(/t=(.*?)%>/g, "',$1,'") // same here
.split("t").join("');")
.split("%>").join("p.push('") // same here
.split("r").join("\'")
Rick Strahl有一个很好的帖子,他把它修改成ASP。. NET友好(微模板已接近尾声;向下滚动):http://www.west-wind.com/weblog/posts/2008/Oct/13/Client-Templating-with-jQuery
您也可以将%符号替换为JavaScript字符串中百分比符号的unicode表示形式'u0025'。所以你应该写"<%=id%>"而不是字符串"<u0025=idu0025>"