Terima kasih atas solusi Ben, kasus penggunaan saya hanya menampilkan bidang tertentu secara berurutan
dengan objek
Kode:
handlebars.registerHelper('eachToDisplayProperty', function(context, toDisplays, options) {
var ret = "";
var toDisplayKeyList = toDisplays.split(",");
for(var i = 0; i < toDisplayKeyList.length; i++) {
toDisplayKey = toDisplayKeyList[i];
if(context[toDisplayKey]) {
ret = ret + options.fn({
property : toDisplayKey,
value : context[toDisplayKey]
});
}
}
return ret;
});
Sumber objek:
{ locationDesc:"abc", name:"ghi", description:"def", four:"you wont see this"}
Templat:
{{#eachToDisplayProperty this "locationDesc,description,name"}}
<div>
{{property}} --- {{value}}
</div>
{{/eachToDisplayProperty}}
Keluaran:
locationDesc --- abc
description --- def
name --- ghi