[英]Unable to render json details using handlebar helper
I'm trying to stringify an object to json and pass it to a template. 我正在尝试将对象字符串化为json并将其传递给模板。 But instead I see whole object as a string rather than only what I want. 但相反,我将整个对象视为一个字符串,而不仅仅是我想要的东西。
Code 码
Handlebars.registerHelper("GetFBUserDetails", function() {
var details = new Object({'fName' : firstName, 'lName' : lastName, 'pic' : pictureUrl});
return JSON.stringify(details);
});
<li>{{#GetFBUserDetails}}
{{fName}}
{{/GetFBUserDetails}}
</li>
I want only the firstname (fName) to be shown in li instead the whole object shows up as a string. 我只想在li中显示firstname(fName)而不是整个对象显示为字符串。
JSON.stringify()
does what it's named after, it takes a JSON object and serializes it into a string. JSON.stringify()
执行它命名的内容,它接受一个JSON对象并将其序列化为一个字符串。 Handlebars uses objects, not strings. 把手使用对象而不是字符串。
This should do it: 这应该这样做:
Handlebars.registerHelper("GetFBUserDetails", function() {
return {'fName' : firstName, 'lName' : lastName, 'pic' : pictureUrl};
});
To apply the template, you need to actually pass the result to the Handlebars processor. 要应用模板,您需要将结果实际传递给Handlebars处理器。 Do this using the parameter (options) of the callback function to the object (not stringified): 使用回调函数的参数(选项)对对象(未进行字符串化)执行此操作:
Handlebars.registerHelper("GetFBUserDetails", function(options) {
var details = new Object({'fName' : firstName, 'lName' : lastName, 'pic' : pictureUrl});
return options.fn(details);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.