How do I get Mustache to render this data
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
I think you need to change
{{data}}
into
{{#data}}
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.
精彩评论