无法从Array生成Mustache HTML

时间:2013-10-11 14:01:38

标签: javascript mustache

var self = this;
self.filters = {
    genres: [],

    ...

};
self.getLibrary = function() {
    $.getJSON("library.json", function(data) {
        for (var i=0, l=data.filters.genres.length; i<l; i++) {
            self.filters.genres.push({genre: data.filters.genres[i]});
        }

        ...

        $.each(self.filters, function(k1,v1) {
            var tmpl = "{{#" + k1+ "}}{{" + k1.slice(0, -1) + "}}{{/" + k1 + "}}";
            var html = Mustache.render(tmpl, self.filters[k1]);
            $("#" + k1).html(html);
        });

        ...

        }
    });
};

记录tmpl变量会产生以下结果:

{{#genres}}{{genre}}{{/genres}}

记录self.filters[k1]会产生如下数组:

[
    {genre: "Ambient"},
    {genre: "Bass"},

    ...

]

记录html变量会产生一个空白字符串。

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

终于解决了问题:

var html = Mustache.render(tmpl, self.filters[k1]);

=&GT;

var html = Mustache.render(tmpl, self.filters);