What is JsRender?
Many development platforms use templates to reduce code and simplify maintenance, and HTML5 and JavaScript are no exception. JsRender is a JavaScript library that allows you to define a boilerplate structure once and reuse it to generate HTML dynamically. JsRender brings a new templating library to HTML5 development that has a codeless tag syntax and high performance, has no dependency on jQuery nor on the Document Object Model (DOM), supports creating custom functions and uses pure string-based rendering.
Why Templates?
Using templates with JavaScript reduces and simplifies code. Without templates, adding a series of list items and other HTML elements for a set of data might require manipulating a browser’s DOM. This is where templating using a plug-in such as JsRender can be quite useful to do the heavy lifting.
<!DOCTYPEhtml><html><head><scriptsrc="js/jquery.min.js"type="text/javascript"></script><scriptsrc="js/jsrender.js"type="text/javascript"></script><linkhref="css/bootstrap.min.css"rel="stylesheet"type="text/css"/><scriptid="book_template"type="text/x-jsrender"><tr><td>{{:#index+1}}</td><td>{{>name}}</td><td>{{>releaseYear}}</td></tr></script><scripttype="text/javascript">$(document).ready(function(){varbooks=[{name:"Erlang",releaseYear:"1986"},{name:"Ruby",releaseYear:"1998"},{name:"Ruby on Rails",releaseYear:"1999"},{name:"Javascript",releaseYear:"1976"}];$("#book_list").html($("#book_template").render(books));});</script></head><body><divclass="container"><sectionid="fluidGridSystem"><divclass="page-header"><h3>Rendertemplateagainstlocaldata</h3></div><divclass="row-fluid show-grid"><tableclass="table table-bordered"><thead><tr><th>No</th><th>Name</th><th>ReleaseYear</th></tr></thead><tbodyid="book_list"></tbody></table></div></section></div></body></html>