0

I'm trying to stringify an object to json and pass it to a template. 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.

4

2 回答 2

1

JSON.stringify()按照它的名字命名,它接受一个 JSON 对象并将其序列化为一个字符串。Handlebars 使用对象,而不是字符串。

这应该这样做:

Handlebars.registerHelper("GetFBUserDetails", function() {
    return  {'fName' : firstName, 'lName' : lastName, 'pic' : pictureUrl};
});
于 2013-10-22T15:44:50.367 回答
1

要应用模板,您需要将结果实际传递给 Handlebars 处理器。使用对象的回调函数的参数(选项)执行此操作(未字符串化):

Handlebars.registerHelper("GetFBUserDetails", function(options) {
    var details = new Object({'fName' : firstName, 'lName' : lastName, 'pic' : pictureUrl});
    return options.fn(details);
});

小提琴

于 2013-10-22T15:54:01.450 回答