最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - John Resig's microtemplating framework throws errors on asp.net web page - Stack Overflow

programmeradmin0浏览0评论

I'm looking into John Resig's microtemplating framework and it is wonderful, small and fits my requirements. The only problem is that the syntax confuses the ASP.NET framework. This is due to the fact that anything put inside the

<%=id%>
<%=(i % 2 == 1 ? " even" : "")%>

expression syntax is evaluated using Server variables. Has anyone hacked/changed the code to work with ASP.NET?

I'm looking into John Resig's microtemplating framework and it is wonderful, small and fits my requirements. The only problem is that the syntax confuses the ASP.NET framework. This is due to the fact that anything put inside the

<%=id%>
<%=(i % 2 == 1 ? " even" : "")%>

expression syntax is evaluated using Server variables. Has anyone hacked/changed the code to work with ASP.NET?

Share Improve this question edited Jul 28, 2011 at 16:24 Justin Morgan 30.6k13 gold badges81 silver badges109 bronze badges asked Jul 28, 2011 at 16:19 DeeptechtonsDeeptechtons 11.1k27 gold badges105 silver badges178 bronze badges 8
  • 1 What exactly is it that doesn't work? I don't see any obvious problem with that, and most server-side frameworks have similar mechanisms. – Pointy Commented Jul 28, 2011 at 16:22
  • @Pointy have you ever used Asp expressions before. From your ment above it seems you haven't :( – Deeptechtons Commented Jul 28, 2011 at 16:25
  • Also, that's an old old blog post, and the template framework is much more evolved than in that post (I think). You should check the github page for the project. – Pointy Commented Jul 28, 2011 at 16:25
  • @Pointy the base template i referred to was Standalone wasn't it? – Deeptechtons Commented Jul 28, 2011 at 16:28
  • 1 @Pointy jQuery template and john resigs micro templating system are two different (independant) things – Raynos Commented Jul 28, 2011 at 16:29
 |  Show 3 more ments

4 Answers 4

Reset to default 9

Just change <% and %> in the parsing function to <# and #>, respectively. I've seen it done, and it works great.

 // 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("\\'")

...etc.

If it's not working because of <% and %>, then you can easily change the source code.

    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("\\'")

Rick Strahl had a good post a while back where he modified it to be ASP.NET friendly (the microtemplating is toward the end; scroll down): http://www.west-wind./weblog/posts/2008/Oct/13/Client-Templating-with-jQuery

You could also replace the % sign with the unicode representation of the percentage sign in JavaScript strings which is '\u0025'. So instead of the string "<%=id%>" you'd write "<\u0025=id\u0025>"

发布评论

评论列表(0)

  1. 暂无评论