Mustache inside of href

倖福魔咒の 提交于 2019-12-12 07:48:18

问题


I have JSON like this:

 { "something": "http://something.com" }

and HTML like this:

 <a href="{{something}}">{{something}}</a>

When I apply Mustache, I get

 <a href="%7B%7Bsomething%7D%7D">http://something.com</a>

But what I am trying to get is

 <a href="http://something.com">http://something.com</a>

I already tried {{{ something}}}, {{& something}}, single quotes, double quotes... I even read documentation.

Can you help me?


回答1:


Make sure your template source is straight text - don't try and grab parsed HTML source as your template. Browsers will urlencode/escape those characters in your link href, and result in those %7Bs and %7Ds you see in your code. Mustache won't recognize that.

I suppose unescaping the source you pass to mustache might work, though.

Mustache.render(unescape(source),view)



回答2:


I think you need to make use of the & for escaping in combination with surrounding your template with a template script:

<script type="text/template" id="tmpl">
    <a href="{{& something }}">{{ something }}</a>
</script>

Found this example over here.



来源:https://stackoverflow.com/questions/11218075/mustache-inside-of-href

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