Handlebars.registerHelper('link', function (text, url) { text = Handlebars.Utils.escapeExpression(text); url = Handlebars.Utils.escapeExpression(url); var result = '<a href="' + url + '">' + text + '</a>'; return new Handlebars.SafeString(result); }); Handlebars.registerHelper('list', function (items, options) { var out = "<ul>"; for (var i = 0, l = items.length; i < l; i++) { debugger; out = out + "<li>" + options.fn(items[i]) + "</li>"; } return out + "</ul>"; }); Handlebars.registerHelper('select', function (items, options) { var select = "<select class='chosen m-wrap " + options.hash.class + "' name=" + options.hash.name + " id=" + options.hash.id + ">"; for (var i = 0, l = items.length; i < l; i++) { select = select + "<option value='" + items[i][options.hash.value] + "'>" + options.fn(items[i]) + "</option>"; } return select + "</select>"; });
<script id="demo" type="text/x-handlebars-template"> {{#select people value="firstName"}} {{firstName}} {{/select }} </script>
var source = $("#demo").html(); var template = Handlebars.compile(source); var users = [{ ID: 'think8848', Name: 'Joseph Chan', Num: '1', Status: 1, people: [ { firstName: "Yehuda", lastName: "Katz" }, { firstName: "Carl", lastName: "Lerche" }, { firstName: "Alan", lastName: "Johnson" } ] }, { ID: 'aCloud', Name: 'Mary Cheung', Num: '2', people: [ { firstName: "Yehuda", lastName: "Katz" }, { firstName: "Carl", lastName: "Lerche" }, { firstName: "Alan", lastName: "Johnson" } ] }]; var html = template(users[0]); $("#div_demo").append(html);
讓眾人的薪枝構起這團熱情的火焰