简体   繁体   中英

Mustache template with Javascript

I'm trying, without success, to add a javascript to a mustache template

<script id="nodeTemplate" type="text/template">
  {{#myItem}}
    <div class="divclass {{#myItemDet}}">
    ...
    </div>


    <script type="text/javascript">
     alert('{{#myItemDet}}');
    </script>

  {{/myItem}}
</script>

Maybe, it is not possible with Mustache to create JS script...

--------------------------- NOTE 1
If a embeed <script type="text/javascript"> ... <script> inside a <script id="nodeTemplate" type="text/template">...</script> I suspect that mustache close the template at the first tag

Is it true?

Riccardo

--------------------------- NOTE 2
This is my solution:

1) fill with mustache several hidden divs with the class jshidden

2) Search objs with class=jshidden and append div-content as javascript

The solution now work only on Chrome... Still working

http://jsfiddle.net/ric79/zWEUK/

You are using the block form for myItemDet , rather than the value form.

Try this

<script id="nodeTemplate" type="text/template">
  {{#myItem}}
    <div class="divclass {{myItemDet}}">
    ...
    </div>


    <script type="text/javascript">
     alert('{{myItemDet}}');
    </script>

  {{/myItem}}
</script>

Maybe try to obfuscate the ending </script> tag to not have the browser's parser take it for an inline script. Split it apart, just like advertisers do when writing to the document (eg document.write('<'+'/script>'); ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM