CURRENT PROJECTS
loading
$(target).jQTemplate(data<object || array>, modifier<object>, template<string || selector>);
var movies = [ {title:'The Last Picture Show', year:'1971', genre:'Drama', rating:'8.1/10'}, {title:'The Dark Knight', year:'2008', genre:'Action', rating:'8.9/10'}, {title:'Pulp Fiction', year:'1994', genre:'Drama', rating:'8.9/10'}, {title:'Raiders of the Lost Ark', year:'1981', genre:'Action', rating:'8.7/10'}, {title:'Aliens', year:'1986', genre:'Sci-Fi', rating:'8.5/10'} ]; $('#blockResult').jQTemplate(movies, {}, '.templates .movieTemplate');Template HTML:
<div class="templates" style="display:none;"> <div class="movieTemplate"> <div class="header"> <div class="title">Title</div> <div class="year">Release Year</div> <div class="genre">Genre</div> <div class="rating">Rating</div> </div> <div class="itr"> <div class="cell"> <div class="title"></div> <div class="year"></div> <div class="genre"></div> <div class="rating"></div> </div> </div> </div> </div>Result:
var movies = [ {title:'The Last Picture Show', year:'1971', genre:'Drama', rating:'8.1/10'}, {title:'The Dark Knight', year:'2008', genre:'Action', rating:'8.9/10'}, {title:'Pulp Fiction', year:'1994', genre:'Drama', rating:'8.9/10'}, {title:'Raiders of the Lost Ark', year:'1981', genre:'Action', rating:'8.7/10'}, {title:'Aliens', year:'1986', genre:'Sci-Fi', rating:'8.5/10'} ]; $.get('demo3-template.html', function (template) { $('#blockResult').jQTemplate(movies, {}, template); });'demo3-template.html' file contents:
<div class="movieTemplate"> <div class="header"> <div class="title">Title</div> <div class="year">Release Year</div> <div class="genre">Genre</div> <div class="rating">Rating</div> </div> <div class="itr"> <div class="cell"> <div class="title"></div> <div class="year"></div> <div class="genre"></div> <div class="rating"></div> </div> </div> </div>
// render top level template var products = [{ type:'software', products:[ {descrip: 'Abobe Photoshop CS4 Professional', price:2099}, {descrip: 'Autodesk AutoCAD LT', price:800} ] }, { type:'hardware', products:[ {descrip: 'Samsung 1TB 7200RPM SATA Hard Drive', price:250}, {descrip: 'Seagate 750GB 7200RPM SATA Hard Drive', price:189} ] }]; $('#result').jQTemplate(products, {}, '.templates .productTypes') // render render next level nested template .find('.product').each(function (productsIndex) { var productsByType = products[productsIndex].products; $(this).jQTemplate(productsByType, { 'price': function (index) { return '$' + productsByType[index].price.toFixed(2); } }, '.templates .productsByType') });Template HTML:
<div class="templates" style="display:none;"> <!-- top level table template --> <table class="productTypes"> <tbody> <tr> <td class="type"></td> <td class="product"></td> </tr> </tbody> </table> <!-- nested table template --> <table class="productsByType"> <thead> <tr> <th>Description</th> <th>Price</th> </tr> </thead> <tbody> <tr> <td class="descrip"></td> <td class="price"></td> </tr> </tbody> </table> </div>Result: