In web development, JS parsing json is a common thing. Very cumbersome. Handlebars uses a template, as long as you define a template, provide a JSON object, handlebars will be able to put the JSON object in your template template
HTML code
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" > js Code
$ (document). Ready (function () {handlebars.registerhelper (' fullName ', function (person) {return pers On.firstname + "" + person.lastname; }); $ ("#btn_simple"). Click (function () {//$ (this). Hide (); Showtemplate (); }); $ ("#btn_helper"). Click (function () {showhowusehelper (); }); }); var context = {title: "My New Post", Body: "This is My first post!"}; var persion = {title: "My New Post", Body: "This is My first post!"} function Showtemplate () {var Source = $ ("#some-template"). html (); var template = handlebars.compile (source); var data = {users: [{username: "Alan", FirstName: "Alan", LastName: "Johnson", Email: "[Email protec Ted] "}, {username:" Allison ", FirstName:" Allison ", LastName:" House ", Email:" [email protected] "}, {username: "Ryan", FirstName: "Ryan", LastName: "Carson", Email: "[emAil protected] "}"}; $ ("#my_div"). HTML (template (data));; } function Showhowusehelper () {var context = {Author: {firstName: "Alan", LastName: "Johns On "}, Body:" I love Handlebars ", comments: [{author: {firstName:" Yehuda ", LastName:" Katz "}, Body:" Me too! " }] }; var Source = $ ("#helper-template"). html (); var template = handlebars.compile (source); $ ("#helper_div"). HTML (template (context));; }
Use of handlebars