内联html的jquery tmpl替代方案?

时间:2012-08-15 19:25:08

标签: javascript jquery jquery-templates

我目前在我的jquery中遇到长内联html,以便动态生成长html片段。 例如:

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吗?

2 个答案:

答案 0 :(得分:1)

我刚刚遇到过这个:t.js

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

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

答案 1 :(得分:0)

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

看起来相当受欢迎。

希望有所帮助。