如何让 Mustache 呈现此数据

发布于 2024-12-09 17:04:53 字数 2393 浏览 0 评论 0原文

我是小胡子模板库的新手,并且有以下场景。我正在从网络服务器检索模板和数据。我尝试使用以下代码将两者结合起来:

function formatSearchResults(results)
{
    dojo.xhrGet({
        url:"search_result_template.mch",
        handleAs:"text",
        load: function(template)
        {
                //Load the template into a hidden div on the page
            var templateNode = dojo.byId("templateArea")
        templateNode.innerHTML = template;
    }
});

var templateNode = dojo.byId("templateArea");
var formattedResults;
formattedResults = Mustache.to_html(templateNode.innerHTML,results);
var templateNode = dojo.byId("outputArea");
outputArea.innerHTML = formattedResults;
}

检索 json 和模板工作正常。但小胡子永远不会渲染我的数据。

我有这个 json 数据:

{
      "data": [
            {
                  "ColumnValues": { 
                        "_0": "Bubbles", 
                        "_1": "Thompson%20Trucking",  
                        "_2": "A633937432DF91212431251256D350", 
                        "_3": "Eagleton", 
                        "_4": "CA", 
                        "_5": "555-555-5555", 
                        "_6": "12121", 
                        "_7": "Midatlantic", 
                        "_8": "Thompson%20Trucking..<snip>, 
                        "_9": ""
                  }
            }, <snip>
              ]
}

和这个胡子模板:

<table id="orgInfo">
    <tr>
        <th>Search results: <br/><br/></th>
    </tr>
    <tr>
        <td>Organization</td>
        <td>City</td>
        <td>State</td>
        <td>Phone</td>
        <td>Region</td>
        <td>Industry</td>
        <td>Description</td>
    </tr>
    {{data}}
  {{#ColumnValues}}

<tr>
    <td>{{_0}}</td>
    <td></td>
    <td></td>
    <td></td>
    <td>Region</td>
    <td>Industry</td>
    <td></td>
</tr>
  {{/ColumnValues}}
  {{/data}}

这是我得到的输出:

{{/data}}
Search results: 

Organization    City    State   Phone   NAICS   Region  Industry    Description
                                    NAICS   Region  Industry`   

我已经阅读文档和示例几个小时了。谁能告诉我我做错了什么?

谢谢!

开尔文

I am new to the mustache template library and have the following scenario. I am retrieving both a template and the data from a web server. I am trying to combine the two using the following code:

function formatSearchResults(results)
{
    dojo.xhrGet({
        url:"search_result_template.mch",
        handleAs:"text",
        load: function(template)
        {
                //Load the template into a hidden div on the page
            var templateNode = dojo.byId("templateArea")
        templateNode.innerHTML = template;
    }
});

var templateNode = dojo.byId("templateArea");
var formattedResults;
formattedResults = Mustache.to_html(templateNode.innerHTML,results);
var templateNode = dojo.byId("outputArea");
outputArea.innerHTML = formattedResults;
}

Retrieving the json and the template work fine. But the mustache never renders my data.

I have this json data:

{
      "data": [
            {
                  "ColumnValues": { 
                        "_0": "Bubbles", 
                        "_1": "Thompson%20Trucking",  
                        "_2": "A633937432DF91212431251256D350", 
                        "_3": "Eagleton", 
                        "_4": "CA", 
                        "_5": "555-555-5555", 
                        "_6": "12121", 
                        "_7": "Midatlantic", 
                        "_8": "Thompson%20Trucking..<snip>, 
                        "_9": ""
                  }
            }, <snip>
              ]
}

and this mustache template:

<table id="orgInfo">
    <tr>
        <th>Search results: <br/><br/></th>
    </tr>
    <tr>
        <td>Organization</td>
        <td>City</td>
        <td>State</td>
        <td>Phone</td>
        <td>Region</td>
        <td>Industry</td>
        <td>Description</td>
    </tr>
    {{data}}
  {{#ColumnValues}}

<tr>
    <td>{{_0}}</td>
    <td></td>
    <td></td>
    <td></td>
    <td>Region</td>
    <td>Industry</td>
    <td></td>
</tr>
  {{/ColumnValues}}
  {{/data}}

and this is the output I get:

{{/data}}
Search results: 

Organization    City    State   Phone   NAICS   Region  Industry    Description
                                    NAICS   Region  Industry`   

I have been over the docs and examples for hours. Can anybody tell me what I am doing wrong?

Thanks!

Kelvin

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

嘿咻 2024-12-16 17:04:53

我认为你需要改变

{{data}}

{{#data}}

I think you need to change

{{data}}

into

{{#data}}
梦里梦着梦中梦 2024-12-16 17:04:53

好的。我现在感觉自己像个傻瓜。在将 json 文本传递给 Mustache 库之前,我没有将其转换为对象。现在一切都很好。我希望这可以帮助其他犯同样愚蠢错误的人。

Ok. I now feel like a dope. I was not converting the json text to an object before passing it to the mustache library. All is well now. I hope this helps someone else who makes the same goofy mistake.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文