문제

I'm having trouble getting nested JavaScript inside Underscore templates to run.

<script type="text/template" id="myTemplate">
   <h3><%= heading %></h3>
   <div class="container"></div>

   //THIS DOES NOT WORK --->
   <script type="text/javascript" charset="utf-8">
     $(".container").html("Test")
   </script>
   //END

</script>

Is this at all possible or how could I work around the issue? Thanks.

도움이 되었습니까?

해결책

Javascript certainly doesn't belong into a template. So it only makes sense that they do not work.

If you need to execute scripts related to the template, put them in a function and call them when you render the template.

다른 팁

<script type="text/template" id="myTemplate">
   <h3><%= heading %></h3>
   <div class="container"></div>

   //THIS WORK :) --->

   <% print("<sc" + "ript type='text/javascript'>"); %>

     $(".container").html("Test")

    <% print("</sc"+"ript>"); %>

   //END

</script>

I had to do so, because I had to generate an unknown number of functions.

I have used this method for templating an html page.

<script type="text/template" id="mypage">
  <!DOCTYPE html>
  <html>
    <body>
      <script type="text/javascript" src="jquery.min.js"><%= "<"+"/script>" %>
    </body>
  </html>
</script>
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top