<! DOCTYPE html>{{#each student}} {{#ifname}} {{#compare age20}} <tr> <td>{{name}}</td> <td>{{sex}}</td> <td>{{a Ge}}</td> </tr> {{Else}} <tr> <td>?</td> <td>?</td> <td>?</td> </tr> {{/compare}} {{/if}} {{/each}} </script> <!--for data processing, HTML construction--&G T <script type= "Text/javascript" >$ (document). Ready (function() { //simulated JSON objects vardata = { "Student": [ { "Name": "Zhang San", "Sex": "0", "Age": 23 }, { "Sex": "0", "Age": 22 }, { "Name": "Niu", "Sex": "1", "Age": 18 } ] }; //register a handlebars template, find a template by ID, get the template HTML frame //$ ("#table-template"). HTML () is the syntax of jquery, do not understand the children's shoes please ... varMyTemplate = Handlebars.compile ($ ("#table-template"). HTML ()); //register a comparison size helper to determine if V1 is greater than V2Handlebars.registerhelper ("Compare",function(v1,v2,options) {if(v1>v2) { //satisfy add to continue execution returnOptions.fn ( This); }Else{ //do not meet condition execution {{else}} section returnOptions.inverse ( This); } }); //The JSON object is encapsulated with the newly registered handlebars template, resulting in the final HTML, inserted into the underlying table. $ (' #tableList '). HTML (myTemplate (data)); }); </script> </body>Many times, we need more complex if-judgment logic, and obviously the default if does not meet our needs.
In this example, using the powerful extensibility of handlebars.js, a compare is defined to compare the size of two numbers, and if the first number is greater than the second number, the condition continues to execute, otherwise the {{else}} portion is executed.
Handlebars.registerhelper is used to define helper, it has two parameters, the first argument is the helper name, and the second parameter is a callback function to execute the core business logic. The function in this example, there are three parameters, where the first two parameters are required to compare the two number, the third parameter is fixed, called the options, if added to this parameter, this helper is a block, block-level helper, there is a certain grammatical structure, called when the # number, Just like if.
With regard to the use of options, it is this usage that is known by the side dishes, return Options.fn (this), indicating that the condition continues to execute, that is, the code between {{#compare}} and {else}} is executed; return Options.inverse (this); indicates that the condition is not met, that is, the code between {{else}} and {{/compare}} is executed.
Since this is a block-level helper, it is called to add #, for example: {{#compare age 20}}, where ages is the current context to read the old, 20 is the value of casually written side dishes, meaning that as long as age than 20, the display, otherwise full display?
Readers can see that the helper can be written in any JS code, now think about it, we know how flexible handlebars!!
Original link http://www.cnblogs.com/iyangyuan/p/3471350.html
JS template engine Handlebars.js Practical tutorial--due to the lack of if skill to elicit helper