0

I am currently running into long inline html in my jquery in order to generate long html snippets on the fly. Example:

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

I want to add even more html in my personalMessage so i started to think that jquery.tmpl will be perfect for this, but jQuery discontinued it which i dont know why, but is there something new or an alternative that everyone uses now thats better? or is everyone still just using jquery.tmpl?

Charles
  • 50,943
  • 13
  • 104
  • 142
anthonypliu
  • 12,179
  • 28
  • 92
  • 154
  • Example: http://stackoverflow.com/questions/3585573/html-templates-in-javascript-without-coding-in-the-page – JayC Aug 15 '12 at 19:29
  • I like [jsRender](https://github.com/BorisMoore/jsrender) for templating, but it's still not even in Beta status, so there could still be breaking changes as development continues – MrOBrian Aug 15 '12 at 19:38

2 Answers2

1

i came across this one just recently: t.js

however, if your needs are fairly limited you could just extend String with a simple template function:

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

Omer Bokhari
  • 57,458
  • 12
  • 44
  • 58
0

If you're looking for an HTML templating language in Javascript, you might want to look into Mustache.js.

It seems fairly popular.

Hope that helps.

Spudley
  • 166,037
  • 39
  • 233
  • 307