简体   繁体   English

无法呈现小胡子模板

[英]Unable to render a Mustache template

I have a template like: 我有一个类似的模板:

{{#items}}
    <div id="{{dataId}}">
        <p>{{firstName}}</p>
        <p>{{sex}}</p>
    </div>
{{/items}}

and an array of objects retrieved from server: 以及从服务器检索到的对象数组:

dataId : 128  firstName : 'john'  sex : 'Male'
dataId : 203  firstName : 'doe'  sex : 'Female'
..

I am trying to render it using Mustache template with render() like: 我正在尝试使用带有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
}

The problem is that the render function is returning blank string as output rather than rendered html (step 2). 问题在于render函数正在返回空白字符串作为输出,而不是渲染的html(步骤2)。

Note: Printing the ajax result in console log, gives Array[12] as output. 注意:在控制台日志中打印ajax结果,将Array [12]作为输出。

You are getting array of object, but mustache needs object to render your template. 您正在获取对象数组,但是小胡子需要对象来呈现您的模板。 And you are updating the script tag which is hidden element, it would not display any html on the page. 并且您正在更新脚本标签,它是隐藏的元素,它不会在页面上显示任何html。 I added new div and working fine now. 我添加了新的div,现在可以正常工作。 Here is the working JSFIDDLE 这是工作中的JSFIDDLE

Mustache.render(template, {items:items});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM