简体   繁体   English

带有预加载模板的Mustache.js

[英]Mustache.js with preloaded template

I use mustache.js in my product and it works like a charm. 我在产品中使用mustache.js,它的工作原理很吸引人。 But it became necessary to to apply mustache to my index page. 但是有必要在我的索引页上涂上胡须。 eg 例如

<html>
   <head>
      <title>{{title}}</title>
      ....

as far i know, the only way to render is Mustache.render(tpl,view) and tpl should be a string. 据我所知,渲染的唯一方法是Mustache.render(tpl,view)和tpl应该是一个字符串。 $('html').html(Mustache.render($('html').html(), view)) looks very ugly. $('html').html(Mustache.render($('html').html(), view))看起来很丑。

Is there an easy way out? 有没有简单的出路? Or i should invent bycicle)? 还是我应该发明自行车)?

@sroes suggested me a jQuery function http://benalman.com/projects/jquery-replacetext-plugin/ Author promisses: "only text content will be modified, leaving all tags and attributes untouched". @sroes向我建议了一个jQuery函数http://benalman.com/projects/jquery-replacetext-plugin/作者承诺:“仅文本内容将被修改,所有标记和属性均保持不变”。

How to use in my case? 在我的情况下如何使用?

Its simple: 这很简单:

$('html *').replaceText(/{{([^}]+)}}/, function(fullMatch, key) {
    return key;
}, true);

See http://jsfiddle.net/4nvNy/ Thank @sroes alot! 看到http://jsfiddle.net/4nvNy/非常感谢@sroes!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM