Using mustache to fill in a html tag argument

三世轮回 提交于 2019-12-19 04:05:40

问题


I'm trying the append an identier (id) to the href below using mustache

Template:

<div id='tmpl'>
    <a href='#product_detail?'{{id}}>link</a>
</div>    

var template = $('#tmpl').html();

Data:

var model = { id: 22 };

Rendering the template using the model data:

var html = Mustache.to_html(template, model);

results in:

<a href="#product_detail?%7B%7Bid%7D%7D">link</a>

If the template is changed to:

<div id='tmpl'>
    <a href='#product_detail?'{{id}}>link</a>
</div>

The resulting template is:

<a href="#product_detail?" 0="">link</a>

The 'problem' seems to be that jQuery is changing the single quotes in the template to double quotes that confuses mustache. Placing the mustache tag outside the quotes doesn't give the right results either. How can this be solved?

Thanks


回答1:


You need to add the {{id}} in the href, like that :

href="#product_detail?{{id}}"

We used to put our mustache template in a script with type="text/template", i don't know if it will solve your escaping problems but we haven't here.

<script type="text/template" id="tmpl">
   <a href href="#product_detail?{{id}}">link</a>
</script>

If you got some escaping problems use the & before the id like that:

<script type="text/template" id="tmpl">
   <a href href="#product_detail?{{& id}}">link</a>
</script>

Hope it help !



来源:https://stackoverflow.com/questions/8137480/using-mustache-to-fill-in-a-html-tag-argument

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!