无法呈现 Mustache 模板
Unable to render a Mustache template
我有一个模板:
{{#items}}
<div id="{{dataId}}">
<p>{{firstName}}</p>
<p>{{sex}}</p>
</div>
{{/items}}
以及从服务器检索到的对象数组:
dataId : 128 firstName : 'john' sex : 'Male'
dataId : 203 firstName : 'doe' sex : 'Female'
..
我正在尝试使用带有 render()
的 Mustache 模板来呈现它,例如:
success: function(items){
var template = $('#movie_template').html(); --- step 1
var output = Mustache.render(template, items); --- step 2
$('#movie_template').html(output); --- step 3
}
问题是 render
函数返回空白字符串作为输出而不是呈现 html(第 2 步)。
注意:在控制台日志中打印 ajax 结果,输出数组 [12]。
您正在获取对象数组,但 mustache 需要对象来呈现您的模板。并且您正在更新隐藏元素的脚本标签,它不会在页面上显示任何 html 。我添加了新的 div,现在工作正常。这是工作 JSFIDDLE
Mustache.render(template, {items:items});
我有一个模板:
{{#items}}
<div id="{{dataId}}">
<p>{{firstName}}</p>
<p>{{sex}}</p>
</div>
{{/items}}
以及从服务器检索到的对象数组:
dataId : 128 firstName : 'john' sex : 'Male'
dataId : 203 firstName : 'doe' sex : 'Female'
..
我正在尝试使用带有 render()
的 Mustache 模板来呈现它,例如:
success: function(items){
var template = $('#movie_template').html(); --- step 1
var output = Mustache.render(template, items); --- step 2
$('#movie_template').html(output); --- step 3
}
问题是 render
函数返回空白字符串作为输出而不是呈现 html(第 2 步)。
注意:在控制台日志中打印 ajax 结果,输出数组 [12]。
您正在获取对象数组,但 mustache 需要对象来呈现您的模板。并且您正在更新隐藏元素的脚本标签,它不会在页面上显示任何 html 。我添加了新的 div,现在工作正常。这是工作 JSFIDDLE
Mustache.render(template, {items:items});