【问题标题】:Mustache inside of hrefhref里面的小胡子
【发布时间】:2012-06-27 01:11:38
【问题描述】:

我有这样的 JSON:

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

和这样的 HTML:

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

当我申请 Mustache 时,我得到了

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

但我想要得到的是

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

我已经尝试过{{{ something}}}{{&amp; something}}、单引号、双引号……我什至阅读了文档。

你能帮帮我吗?

【问题讨论】:

  • 这里还需要三个大括号,防止“://”转义

标签: javascript templates mustache


【解决方案1】:

我认为您需要使用&amp; 进行转义,并结合使用模板脚本包围您的模板:

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

找到这个例子over here

【讨论】:

  • 另外还有三重大括号 - {{{ something }}} 是另一种语法
【解决方案2】:

确保您的模板源是纯文本 - 不要尝试将已解析的 HTML 源作为您的模板。浏览器将对链接href中的这些字符进行urlencode/escape,并导致您在代码中看到的%7Bs%7Ds。小胡子不会承认这一点。

不过,我想取消转义传递给 mustache 的源代码可能会奏效。

Mustache.render(unescape(source),view)

【讨论】:

  • 谢谢!逃避实际上有所帮助。原来我在将模板提供给 Mustache 之前使用 jQuery 对模板进行了一些转换(我基本上做了这样的事情:$("{{something}}" ) ),这就是为什么这段 HTML 被转义了
猜你喜欢
  • 1970-01-01
  • 2011-08-29
  • 1970-01-01
  • 2013-03-31
  • 2014-11-16
  • 2012-01-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多