【问题标题】:Alternative to jquery tmpl for inline html?替代 jquery tmpl 用于内联 html?
【发布时间】:2012-08-15 19:25:07
【问题描述】:

我目前在我的 jquery 中遇到长内联 html,以便动态生成长 html sn-ps。 示例:

var personalMessage = $("<div title='" + chatid + "' class='personalMessage'><div class='personalChatName'>" + fullname + "</div><div class='personalChatDialog'></div></div>")
$ContactsBar.prepend(personalMessage);

我想在我的personalMessage 中添加更多的 html,所以我开始认为 jquery.tmpl 将非常适合这个,但 jQuery 停止了它,我不知道为什么,但是有什么新的或替代的每个人现在使用那更好?还是每个人都还在使用 jquery.tmpl?

【问题讨论】:

标签: javascript jquery jquery-templates


【解决方案1】:

我最近才遇到这个:t.js

但是,如果您的需求相当有限,您可以使用简单的 template 函数扩展 String

String.prototype.template = function(obj) {
    return this.replace(/\{\{([\w]+)\}\}/g, function(str, prop) {
        return obj[prop];
    });
};

alert('<div>my name is: {{last}}, {{first}}</div>'.template({first:'John', last:'Smith'}));

check it out

【讨论】:

    【解决方案2】:

    如果您正在寻找 Javascript 中的 HTML 模板语言,您可能需要查看 Mustache.js

    它似乎很受欢迎。

    希望对您有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-08-02
      • 2014-02-23
      • 2018-07-31
      • 1970-01-01
      • 1970-01-01
      • 2013-02-27
      • 1970-01-01
      相关资源
      最近更新 更多