Search code examples
underscore.jsunderscore.js-templating

underscore template conflicts in asp.net page


Here is a underscore template whose chances to work within a Asp.net page are weak. Since <% is understood as opening tag for asp.net ( MVC and WebForm) code, can someone tell me how do I insert the following code in a asp.net page?

 <script type="text/html" id='div-data'>
  <% _.each(items,function(item,key,list){ %>
    <div><%= key %></div>
    <div><%= item.name %></div>
  <% }) %>
 </script>

Solution

  • Almost solved! I'd like to post this answer because It may help others...

    I had to configure underscore to tell him that he must escape the asp.net render function <% %>

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

    Now I can use {% %} instead. Thank you