How to use underscore/javascript templates in ASP.Net MVC views

前端 未结 1 1915
迷失自我
迷失自我 2021-01-21 06:15

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

相关标签:
1条回答
  • 2021-01-21 06:23

    From the fine manual:

    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.

    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
    };
    

    Demo: http://jsfiddle.net/ambiguous/TfB5M/

    0 讨论(0)
提交回复
热议问题