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

Including javascript within mustache template - Stack Overflow

programmeradmin1浏览0评论

I have a block of javascript for displaying adverts that I wish to include every nth time on a page.

I'm using Mustache as my templating language but cannot work out how to include the js so it runs as a script rather than just being inserted as a string.

<script id="mustache-post-advert" type="text/mustache">
<article id="post-{{id}}" class="post">

    {{{  <script type="text/javascript">GA_googleFillSlot("MPU")</script>  }}}

</article>

</script>

I've tried triple { which I hoped would escape but sadly it didn't.

I have a block of javascript for displaying adverts that I wish to include every nth time on a page.

I'm using Mustache as my templating language but cannot work out how to include the js so it runs as a script rather than just being inserted as a string.

<script id="mustache-post-advert" type="text/mustache">
<article id="post-{{id}}" class="post">

    {{{  <script type="text/javascript">GA_googleFillSlot("MPU")</script>  }}}

</article>

</script>

I've tried triple { which I hoped would escape but sadly it didn't.

Share Improve this question asked Jul 3, 2012 at 15:40 BobFlemmingBobFlemming 2,04011 gold badges43 silver badges59 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 18

Your json-input should reference the script to call:

JSON

 var json = {
   id: "someid",
   gaFillSlot: function(){
     GA_googleFillSlot("MPU");
   }
 }

TEMPLATE

 <script id="mustache-post-advert" type="text/mustache">
   <article id="post-{{id}}" class="post">
     {{gaFillSlot}}
   </article>
 </script>

Make sure 'GA_googleFillSlot' is available to be called from the context of mustache at time of templating.

This is in line with the logic-less nature of Mustache: any logic you want should be embedded into the json you pass to Mustache to bgin with.

Didn't test this, but this should work. HTH

You're already inside a script tag, there's no need to add another:

<script id="mustache-post-advert" type="text/mustache">
  <article id="post-{{id}}" class="post">
    {{ GA_googleFillSlot("MPU") }}
  </article>
</script>

edit

on second thought, this may not be enough; see here for info on calling functions from within mustache templates:

http://net.tutsplus.com/tutorials/javascript-ajax/quick-tip-using-the-mustache-template-library/

发布评论

评论列表(0)

  1. 暂无评论