我正在调查John Resig's microtemplating framework,这很精彩,很小,符合我的要求。唯一的问题是语法混淆了ASP.NET框架。这是因为任何事情都放在了
之内<%=id%>
<%=(i % 2 == 1 ? " even" : "")%>
使用服务器变量评估表达式语法。是否有人攻击/更改了代码以使用ASP.NET?
答案 0 :(得分:9)
只需将解析功能中的<%
和%>
分别更改为<#
和#>
。我已经看过它了,效果很好。
// Convert the template into pure JavaScript
str
.replace(/[\r\t\n]/g, " ")
.split("<#").join("\t")
.replace(/((^|\#>)[^\t]*)'/g, "$1\r") //note the extra \ here
.replace(/\t=(.*?)\#>/g, "',$1,'") //and here
.split("\t").join("');")
.split("#>").join("p.push('")
.split("\r").join("\\'")
...等
答案 1 :(得分:2)
如果由于<%
和%>
而无效,那么您可以轻松更改源代码。
str
.replace(/[\r\t\n]/g, " ")
.split("<%").join("\t") // this % you could change to @ or whatever
.replace(/((^|%>)[^\t]*)'/g, "$1\r") // same here
.replace(/\t=(.*?)%>/g, "',$1,'") // same here
.split("\t").join("');")
.split("%>").join("p.push('") // same here
.split("\r").join("\\'")
答案 2 :(得分:2)
Rick Strahl在一段时间内发表了一篇好文章,他将其修改为ASP.NET友好版(微模板即将结束;向下滚动):http://www.west-wind.com/weblog/posts/2008/Oct/13/Client-Templating-with-jQuery
答案 3 :(得分:1)
您还可以将%符号替换为JavaScript字符串中百分号的unicode表示形式,即&#39; \ u0025&#39;。因此,而不是字符串&#34;&lt;%= id%&gt;&#34;你写&#34;&lt; \ u0025 = id \ u0025&gt;&#34;