Reputation: 4168
I've done a lot of server-side HTML programming and used a number of different template languages for producing (X)HTML. This part is very clear to me.
But what I'm wondering a bit about is how do people use this in client-side JavaScript programs? I mean, obviously there can be template languages written for JavaScript that work inside the browser - but is that how people normally do it? Or do people just manipulate the DOM directly? Or are there some nice helper libraries most people use?
As an example - let's say I want a JavaScript application that fetches a list of contact cards from the remote server, returned as a JSON object. Then I want to show this contact cards on the browser side using certain predefined HTML markup, to which I fill in the required fields. How is this normally done?
Upvotes: 2
Views: 623
Reputation: 18275
If you are using Script# you may want to consider SharpTemplate, a strongly typed, super efficient HTML templating engine.
Upvotes: 0
Reputation: 4168
I went over the solutions proposed here, and was not entirely satisfied. What I learned was:
elem.innerHTML
. This is too ugly for me.Builder
objects, so in fact they are writing HTML with JavaScript for the dynamic content. This is what I'd use if the content was 100% dynamic, but in this case I'd rather have templates that are actually html that I can customize.But, what I found out to really want myself is for a programmatic way to piece together HTML from DOM snippets, and filling in the required data in the middle. I found two solutions for this:
Of these, I picked PURE for my needs.
Thank you for all the answers.
Upvotes: 1
Reputation: 3757
I suggest ExtJS XTemplates. Simple, powerful, self-contained, nice. -j
Upvotes: 0
Reputation: 8590
The task you describe could be accomplished by a templating system or by manual DOM manipulation, just as doing it on the server side could be accomplished with a templating system or by a series of string concatenations. Personally, if I have to ask this question, I generally go with a templating system. (In fact, if you consider the printf-style formatting available in most languages to be a template system, I hardly ever manipulate strings without using templates.)
A couple of JavaScript template languages I am aware of include:
Closure templates are interesting in that there is a server-side renderer for them available as well, so you can share templates between the server-side code and the JavaScript. jugl is pretty similar to TAL so you may be able to do this with jugl as well.
Upvotes: 1
Reputation: 17544
There are a variety of ways people normally do this:
There are other solutions too, such as hiding the content to be teplated in a textarea or in html script tags, but I'm not a fan of them as they tend to produce incorrect/invalid markup.
Upvotes: 1
Reputation: 86196
You're right. There are template libraries for JavaScript. If it's simple enough, the HTML can just be created as a string and added to the DOM, at which point the browser will display it.
Here's John Resig's micro templating library: http://ejohn.org/blog/javascript-micro-templating/
Upvotes: 0