About 2 years ago (November 2007) I wrote a small JavaScript function to request JSON data from a URL and bind the results to a client-side, HTML-resident template. I used it a demo piece, showing that even though our platform product used (server-side) templates to render it's markup, those template could contain anything, such as JSON to push to a client. There are quite a few of these around now, but I'm still using my own as it's small, I understand how it works (I hope) and recently I used it as an excuse to get into jQuery plugin authoring, modifying my adhoc function to become a plugin. Here are the quickstart four steps to using it and a full example download can be found at the bottom of the page (as soon as I've uploaded it).

Step 1 : Include Required Libraries

<script type="text/javascript" src="/scripts/jquery-1.3.2.min.js">
<script type="text/javascript" src="/scripts/jquery.templatebinder.js">

Step 2 : Add Destination Markup

This basically means, when do you want the bound template contents to end up? I usually include an empty <div>.

<div id="template_destination"></div>

Be sure to include both opening and closing tags, so that the innerHtml can be targeted.

Step 3: Define Your Templates

The trick to including templates in HTML is to prevent the browser thinking they are HTML. Typically, templates are fragments of HTML and therefore unlikely to be compliant. For instance, partial tables, <TR> rows, etc. So, thanks to John Resig for this next tip:

<script type="text/html" id="table_header">
<table>
<thead>
<tr>
<th>Name</th>
<th>Address</th>
<th>Date Of Birth</th>
</tr>
</thead>
<tbody>
</script>

<script type="text/html" id="tablerow_template">
<tr>
<td><span>{{Name}}</span></td>
<td><span>{{Address}}</span></td>
<td><span>{{DateOfBirth}}</span></td>
</tr>
</script>

<script type="text/html" id="table_footer">
</tbody></table>
</script>

The header and footer elements are optional.

Step 4 : Call the Binder

$("#template_destination").templateBinder({
url: "/data/feed.json",
template: "tablerow_template",
header: "table_header",
footer: "table_footer"
});

There are a few extra options that can be passed into the binder, such as whether the template destination is first emptied. Here is a link to the code (21.94 kb).


Comments

suedentist United States

Friday, July 24, 2009 10:53 PM

suedentist

I like your blog. What kind of engine do you use?

brettnagy United States

Thursday, July 30, 2009 12:45 AM

brettnagy

I use BlogEngine.NET

repair your xbox 360 Nicaragua

Saturday, August 08, 2009 7:52 AM

repair your xbox 360

You have a point. Very insightful. A nice different perspective

Lån Denmark

Tuesday, August 11, 2009 5:28 AM

Lån

Great write up - Thank you for sharing..

beginner golf instruction Iceland

Sunday, August 23, 2009 12:13 AM

beginner golf instruction

Thanks for sharing it with us!

payday loans United States

Monday, September 14, 2009 1:09 PM

payday loans

Hi there, I found your blog on Google while seeking for first aid for a heart attack and your post looks very interesting for me.

payday loans United States

Monday, September 14, 2009 1:09 PM

payday loans

In searching for sites related to web hosting and specifically comparison hosting linux plan web, your site came up. Smile

payday loans United States

Sunday, November 22, 2009 3:24 PM

payday loans

I always wanted to write in my site something like that but I guess you'r faster Smile

fast cash loans United States

Friday, December 18, 2009 10:23 AM

fast cash loans

thanks!  very helpful post!! like the template btw ;)

Nebraska Personal Loans United States

Friday, January 01, 2010 1:04 AM

Nebraska Personal Loans

I always wanted to write in my site something like that but I guess you'r faster Smile

payday loans United States

Wednesday, January 27, 2010 6:47 AM

payday loans

It's not the will to win, but the will to prepare to win that makes the difference.

Yong Croke People's Republic of China

Saturday, February 13, 2010 10:16 PM

Yong Croke

For some reason only half from the post is being displayed, is it my browser or the site?

love calculator United States

Sunday, February 14, 2010 3:39 PM

love calculator

Hi, I was looking on the net  and I found your site. Keep up the great work.

aloe products United States

Tuesday, February 16, 2010 4:00 AM

aloe products

Great thread.  Enjoyed the posts..

std testing United States

Saturday, February 20, 2010 10:27 AM

std testing

Do you think you have STD symptoms?  See pictures here http://www.std-symptom.info and to find STD testing.

Fatcow United States

Monday, February 22, 2010 5:26 AM

Fatcow

Should I get a Dedicated Hosting? Right now I am using godaddy but they keep shutting me down because of high server overload. Im getting about 3,000 unique views a day. What brand should I get?

quit smoking programs United States

Monday, February 22, 2010 5:10 PM

quit smoking programs

Need a quit smoking program http://stop-smokingprograms.com  Take a look!  Stop smoking and kick the habit.

chips zynga United States

Tuesday, February 23, 2010 4:43 AM

chips zynga

jeez a couple of of the remarks most people make crack me up, again and again i ponder whether they in actual fact read the subject matter and content before writing a comment or if perhaps they simply just read the title of the blog post and post the initial thought that pops into their heads. in any event, it is actually useful to browse through sensible commentary occasionally instead of the same, traditional blog vomit that i generally , observe on the web

remove acne scars People's Republic of China

Tuesday, February 23, 2010 7:38 PM

remove acne scars

Hi I found your site by mistake when i was searching yahoo for this&nbsp;issue, I have to&nbsp;say your site is really helpful I also love the theme, its amazing!. I don't have the time at the moment to fully read your site&nbsp;but I have bookmarked your site&nbsp;and also add your RSS feeds. I will be back in a day or two. thanks for a great site.

lose weight United States

Wednesday, February 24, 2010 2:33 PM

lose weight

The desire not to be anything is the desire not to be.

facebook chips United States

Wednesday, February 24, 2010 8:10 PM

facebook chips

i was beginning to contemplate i might possibly be the only man which cared about this, at the very least at this point i realize i'm not weird Smile i will make it a point to go and visit a handful of various blogposts right after i get some caffeine in me, it is not easy to read without having my coffee, take care Smile

Russ Nehlsen United States

Thursday, February 25, 2010 3:14 AM

Russ Nehlsen

Hi thanks a lot for a discerning post, I really found your blog by mistake while searching on Goole for something else closely related, in any case before i ramble on too much i would just like to say how much I loved your post, I have bookmarked your site and also taken your RSS feed, Once More thank you very much for the blog post keep up the good work.

Phangan Hotels Germany

Friday, February 26, 2010 12:25 AM

Phangan Hotels

Hi mate
Nice post.
Another nice blog Google showed me. Subscribed to the RSS feed
I will be visiting this site again

Skate 3 Release Germany

Friday, February 26, 2010 12:34 AM

Skate 3 Release

Blogroll links aint that great Tong but i am not the admin… Tong … Just Telling Tong

wow mobile United States

Friday, February 26, 2010 7:43 AM

wow mobile

WoW Mobiles is awesome! I get free mobile service with t-mobile because I refered 3 people to wow. You can too!

free hd movies United States

Friday, February 26, 2010 9:39 PM

free hd movies

I like watching movies online, it is way easier than going to the theaters.

hd movies United States

Saturday, February 27, 2010 6:57 PM

hd movies

I like streaming movies online, it is way easier than going to the theaters.

watch movies online free United States

Sunday, February 28, 2010 10:20 AM

watch movies online free

I like streaming movies online, it is way easier than going to the theaters.

free Farmville cash People's Republic of China

Monday, March 01, 2010 8:53 AM

free Farmville cash

Nice website! I adore a few of the articles which were written, and particularly the comments posted! I will definately be visiting again!

Free Mobile Service United States

Monday, March 01, 2010 5:48 PM

Free Mobile Service

Is tethering against the TOS of cell phone providers? Which ones allow it?

Fletcher Cacatian United States

Tuesday, March 02, 2010 5:53 AM

Fletcher Cacatian

Though I would've loved it much more if you added a relevant video or at least pictures to back up the explanation, I still thought that your write-up quite helpful. It's usually hard to make a complicated matter seem very easy. I enjoy your weblog and will sign up to your feed so I will not miss anything. Fantastic content

acne causes People's Republic of China

Thursday, March 04, 2010 4:12 AM

acne causes

Hello from Spain! May i quote a post as part of your weblog using the link to you? I've tried emailing you about this problem but it appears i cant reach you, please reply when have a moment, thanks.

skate 3 vorbestellen Germany

Friday, March 05, 2010 7:44 PM

skate 3 vorbestellen

Wow! what an idea ! What a concept ! Beautiful .. Amazing …

WoW Mobile United States

Friday, March 05, 2010 8:34 PM

WoW Mobile

Do you know which cell phone services allow tethering?

WoW Mobile United States

Saturday, March 06, 2010 9:45 PM

WoW Mobile

Do you know which mobile phone companies allow tethering?

Monkey Baby Bedding United States

Monday, March 08, 2010 10:07 PM

Monkey Baby Bedding

Bedankt voor de blog geladen met zo veel informatie

runtime error 429 People's Republic of China

Tuesday, March 09, 2010 7:46 PM

runtime error 429

hey, your post&nbsp;actually assists, today i happen the same problems, and i have no idea on how to solve the issue. luckily i research bing and discovered your post, it helps me get rid of my trouble.
thanks once againjust one thing, may i paste your entry on my blog? i will add the source.regards!

Idell Narasimhan United States

Wednesday, March 10, 2010 4:12 AM

Idell Narasimhan

Completely understand what your stance in this matter. Although I would disagree on some of the finer details, I think you did an awesome job explaining it. Sure beats having to research it on my own. Thanks

Michael Pomar United States

Wednesday, March 10, 2010 6:03 AM

Michael Pomar

Just found this blog but I was really looking for some help on how I can recover data from my xbox hdd which seems to have crashed, it's making a funny noise

aussie chat Australia

Thursday, March 11, 2010 2:09 AM

aussie chat

Hi, this was a really quality article. I'd like to blog like this also - taking time and real effort to make a good article... but what can I say... I procrastinate alot and never seem to get something done.

Lastminutereisen Austria

Friday, March 12, 2010 11:10 AM

Lastminutereisen

Brilliant stuff thanx Smile

banners United States

Saturday, March 13, 2010 1:12 AM

banners

Aw, this was a really quality post. In theory I'd like to write like this also - taking time and real effort to make a good article... but what can I say... I procrastinate alot and never seem to get anything done.

bleach anime People's Republic of China

Monday, March 15, 2010 2:56 AM

bleach anime

great article,but i experience some trouble in understanding the final paragraph, can you please make it clear a little bit indepth?

HD Movies Online United States

Friday, March 19, 2010 2:37 PM

HD Movies Online

Do you know where I can watch hd movies online?

Santina Borde United States

Friday, March 19, 2010 11:36 PM

Santina Borde

This is certainly my initial stop by and I really like what I'm seeing. Your weblog is so much fun to look over, quite compelling as well as informative. I'll undoubtedly recommend it to my friends. Nevertheless, I did have some problem with the commenting. It kept giving me an problem whenever I clicked on publish comment. I hope, that can be fixed. Many thanks

online personal loans United States

Sunday, March 21, 2010 11:36 AM

online personal loans

Leaders aren't born they are made. And they are made just like anything else, through hard work.

Comments are closed

Brett Nagy

Software development, engineering and everything in between