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

javascript - Rendering Underscore variables in Jade - Stack Overflow

programmeradmin3浏览0评论

I have included the assets described in this ticket, and the Underscore variables work except when inside tags. I can not get variables to render inside dynamic tags data-id=someidfor doing things onClick with Backbone events.

In standard HTML:

<script type="text/template" id="template-action1-thing">
<tr>
   <td class="action-td" style="width: 10%;">
      <button id="do-remove" data-id="<%= obj.id %>">X</button>             
   </td>
</tr>
</script>

With (Scalate) Jade, which doesn't work:

script(id='template-action1-thing' type='text/template')
  p <%= obj.id %> Will render
  tr
    td.action-td(style='width: 10%;')
      button(id='do-remove' data-id='<%= obj.id %>') 
        | X

If I do this, the actual html renders with the variable properly, though incorrectly:

tr td(style='width: 10%;') button(id='do-remove_thing' data-id='myid') X

With a template like:

script(id='template-action1-thing' type='text/template')
  |   td.action-td(style='width: 10%;')
  |     button(id='do-remove_thing' data-id='<%= obj.id %>') X 

I have included the assets described in this ticket, and the Underscore variables work except when inside tags. I can not get variables to render inside dynamic tags data-id=someidfor doing things onClick with Backbone events.

In standard HTML:

<script type="text/template" id="template-action1-thing">
<tr>
   <td class="action-td" style="width: 10%;">
      <button id="do-remove" data-id="<%= obj.id %>">X</button>             
   </td>
</tr>
</script>

With (Scalate) Jade, which doesn't work:

script(id='template-action1-thing' type='text/template')
  p <%= obj.id %> Will render
  tr
    td.action-td(style='width: 10%;')
      button(id='do-remove' data-id='<%= obj.id %>') 
        | X

If I do this, the actual html renders with the variable properly, though incorrectly:

tr td(style='width: 10%;') button(id='do-remove_thing' data-id='myid') X

With a template like:

script(id='template-action1-thing' type='text/template')
  |   td.action-td(style='width: 10%;')
  |     button(id='do-remove_thing' data-id='<%= obj.id %>') X 
Share Improve this question edited May 23, 2017 at 12:24 CommunityBot 11 silver badge asked Nov 29, 2012 at 23:22 user375566user375566 1
  • Try adding a | to beginning of every line in your template as this gist seems to suggest. – philipvr Commented Nov 30, 2012 at 0:02
Add a ment  | 

3 Answers 3

Reset to default 11

I know this question has already been answered, but in searching for a more eloquent solution, I found that this also works:

script(type='text/html', id='tpl-name')
  h3!='<%= foo %>'
  p!='<%= bar %>'

This allows you to keep using Jade syntax.

If you want to use an underscore template in jade you need change to template to look like this:

script(id='template-action1-thing' type='text/template')
  | <tr>
  |   <td class="action-td" style="width: 10%;">
  |     <button id="do-remove" data-id="<%= obj.id %>">X</button>             
  |   </td>
  | </tr>

Or you could look at using jade templates instead of underscore templates.

Now we can use script. to insert a block of plain text as described here: http://jade-lang./reference/plain-text/.

script(id='template-action1-thing' type='text/template').
    <tr>
       <td class="action-td" style="width: 10%;">
          <button id="do-remove" data-id="<%= obj.id %>">X</button>             
       </td>
    </tr>
发布评论

评论列表(0)

  1. 暂无评论