Use of handlebars

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.