如何使用ASP.Net MVC的观点下划线/ JavaScript的模板 [英] How to use underscore/javascript templates in ASP.Net MVC views

查看:147
本文介绍了如何使用ASP.Net MVC的观点下划线/ JavaScript的模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是想知道你是如何在自℃的视图的.aspx使用下划线模板;即强调通过使用在.aspx渲染引擎得到回升,并给我的错误%=%>标记。

I was just wondering how you use the underscore templates in a .aspx view since the <%= %> tags that underscore uses get picked up by the .aspx rendering engine and give me errors.

例如:

<script type="text/template" id="my-template">
  <span class="event" title="<%= description %>">
      <%= title %>
  </span>
</script>

这个模板给了我,因为在.aspx渲染引擎错误以为我想这个东西绑定到模型。

This template gives me an error since the .aspx rendering engine thinks I'm trying to bind this stuff to the Model.

感谢。

推荐答案

精细的手工

模板 _。模板(templateString,[数据],[设置])结果
  [...]结果
  如果ERB风格的分隔符是不是你的那杯茶,你可以改变下划线的模板设置,使用不同的符号来衬托​​插code。定义一个插值正则表达式匹配,应该逐字插入,前pressions一个逃跑正则表达式匹配应该是HTML逃脱后插入前pressions,和评估正则表达式来匹配,而不应该被插入到评估结果字符串前pressions。

template _.template(templateString, [data], [settings])
[...]
If ERB-style delimiters aren't your cup of tea, you can change Underscore's template settings to use different symbols to set off interpolated code. Define an interpolate regex to match expressions that should be interpolated verbatim, an escape regex to match expressions that should be inserted after being HTML escaped, and an evaluate regex to match expressions that should be evaluated without insertion into the resulting string.

因此​​,如果默认的&LT;%= ...%&GT; &LT;%-...%GT; &LT;%...%GT; 分隔符不为你工作,那么你可以使用不同的用一个简单的配置更改。例如,如果你想使用 {%...%} 而不是&LT;%...%GT; ,那么做到这一点后, underscore.js 加载并使用 _模板:

So if the default <%=...%>, <%-...%>, and <%...%> delimiters aren't working for you then you can use different ones with a simple configuration change. For example, if you wanted to use {%...%} instead of <%...%>, then do this after underscore.js is loaded and before you use _.template:

_.templateSettings = {
    interpolate: /\{%=(.+?)%\}/g,
    escape:      /\{%-(.+?)%\}/g,
    evaluate:    /\{%(.+?)%\}/g
};

演示: http://jsfiddle.net/ambiguous/TfB5M/

这篇关于如何使用ASP.Net MVC的观点下划线/ JavaScript的模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆