我在json中对来自数据库的响应进行了编码,得到了一个带有这个结构的json,我用带有json_encode的控制器发送了echo(我在php中工作):
[
{column1: value, column2: value...} //row 1
{column1: value, column2: value...} //row 2
.... //row n
]
我的脚本(我在车把上的模板)是:
<script id="handlebars_deals_list" type="text/x-handlebars-template">
{{#each data}}
{{tittle}}
{{/each}}
</script>
我以这种方式传递了上下文:
var source=jQuery('#handlebars_deals_list').html();
var template = Handlebars.compile(source);
var context={data:response};//response is the json data I showed earlier
console.log(context); //Object{data="\n[{"id":"149417","biz_n......null,"index_deal":"0"}]"}
var html=template(context);
console.log(html); //empty!!?!?!?!!?!?!?!?!, why????
但我没有看到任何模板呈现
答案 0 :(得分:1)
function process_deals_date(response){
var response=jQuery.parseJSON(response); //THIS IS THE KEY
var source=jQuery('#my_template').html();
var template = Handlebars.compile(source);
Var html=template(response);
console.log(html); //RIGHT POPULATED THE TEMPLATE
}