Quick Tip: An Introduction to jQuery Templating
Tutorial Details
- Technology: jQuery
- Length: 5 Minute Video
JavaScript Templating is a neat idea: it allows you to easily convert JSON to HTML without having to parse it. At Microsoft’s MIX10 conference, they announced that they are starting to contribute to the jQuery team. One of their efforts is to provide a templating plugin. In this quick tip, I’ll show you how to use it!
You’ll need the data to template; you’ll likely retrieve JSON from your server; of course, Object / Array literals work just as well, so that’s what we use:
var data = [
{ name : "John", age : 25 },
{ name : "Jane", age : 49 },
{ name : "Jim", age : 31 },
{ name : "Julie", age : 39 },
{ name : "Joe", age : 19 },
{ name : "Jack", age : 48 }
];
The template is written in <script type="text/html"></script> tags; for each item in your JSON, the template will render the HTML; then, it will return the entire HTML fragment to you. We can get to the JavaScript values from within the template by using {% and %} as tags. We can also execute regular JavaScript within these tags. Here’s our template:
<li>
<span>{%= $i + 1 %}</span>
<p><strong>Name: </strong> {%= name %}</p>
{% if ($context.options.showAge) { %}
<p><strong>Age: </strong> {%= age %}</p>
{% } %}
</li>
To render the data with the template, call the plugin; pass the data to the plugin method; you can optionally pass in an options object as well. (These aren’t predefined options; they’re values you want to use within the template, perhaps for branching.)
$("#listTemplate").render(data, { showAge : true }).appendTo("ul");
It’s that easy! Have fun with it! You can get the complete code for this quick tip on Github
- Eric
- http://ds.laroouse.com piyanistll
- http://www.avey.de Way
- http://www.myunv.com/ Sunny Singh
- http://wpcanyon.com Slobodan Kustrimovic
- http://www.myunv.com/ Sunny Singh
- Strife
- Dom
- http://markscott.co.nz Mark Scott
- http://jaytillery.com Wordpress Wiz
- Dalibor Sojic
- http://markscott.co.nz Mark Scott
- flyingfox
- http://tween.ir/ Hassan
- http://www.yumji.com Benjamin
- Nuno Santos
- http://www.angelbotto.com AngelBotto
- http://twitter.com/michalkozak Michal Kozak
- http://www.bigseadesign.com Adam
- Carlos
- David Runion
- http://www.sonergonul.com/ Soner Gönül
- http://www.engram.nu Niklas Larsson
- http://twitter.com/xrommelx xRommelx
- http://www.farotech.com/ dP
- http://www.enome.be Pickels
- http://twitter.com/javizf nkm
- http://michael.theirwinfamily.net Michael
- http://www.cbesslabs.com Bratu Sebastian
- http://www.twitter.com/mvrilo Murilo
- Stevan
- http://twitter.com/DavGarcia David Garcia
- Wes
- Wes
- http://www.enome.be Pickels
- Khalil
- Pachito Marco Calabrese
- bad bad bad
- http://felixb.se Felix
- http://www.argyllstudios.com Tim
- http://www.tutarli.org Ali Görkem
- http://Todotweet.com Alex wolfe
- Nirali
- http://www.facebook.com/profile.php?id=100000494066687 Kostas
- http://www.jeffadams.co.uk Jeff Adams
- http://www.globalgrafiks.com saqib
- http://webgeekph.com WebGeek Philippines
- http://spotdex.com Davidmoreen
- Yep
- MUhammad Jamil
- http://www.tenaxtechnologies.com java developer
- http://www.jordanwalker.net Jordan Walker
- http://techravings.com Jaro
- http://digitalfunction.com JeffryMckay
- http://ahmedelbaz.com Ahmed Elbaz
- http://www.notifywire.com Ian Drake
- http://www.alexsandro.com.br Alexsandro
- http://crainbandy.com bryan
- Ronny
- http://jazzphotograph.com bali wedding photography
- http://www.visualwebeffects.com Hobbes
- eponymous23
- http://twitter.com/jeffreynolte Jeffrey Nolte
- Rich
- http://sarfraznawaz.wordpress.com/ Sarfraz Ahmed
- http://www.soltani.dk Matt Soltani
- http://www.gigamountz.com/terms_of_service Susan
