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 badges2 Answers
Reset to default 18Your 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/