将动态创建的JSON加载到Moustache模板中

时间:2019-01-08 17:19:42

标签: json mustache

我正在尝试将json加载到我的胡子模板中。问题是:我的服务器中没有.json文件,但可以通过Google工作表创建它。

我使用tabletop.js从Google表格获取数据,并且可以正常工作。我有一个工作的胡子模板。

这是我正在使用的脚本。你能指出我正确的方向吗?

var publicSpreadsheetUrl = 'https://docs.google.com/spreadsheets/d/1jr7C7ec9HIlgfEdSYVs-kL_6DMsR_65AiQ9_Yepi3T0/edit?usp=sharing';
var count = 0;

function init() {
    Tabletop.init({
        key: publicSpreadsheetUrl,
        callback: showInfo,
        simpleSheet: true
    })
}
window.addEventListener('DOMContentLoaded', init);


//this prints the data into the template but using a .json file in the server (which I will not have in production)
$(document).ready(function(){
    $.when($.ajax({url: "../mustache/receta.mst", dataType: 'text'}),$.ajax({url: "../mustache/data-BD.json"}))
    .done(function(template, data){
        Mustache.parse(template[0]);
        var rendered = Mustache.render(template[0], {receta: data[0].receta});
        $(".container").html(rendered);
    })
});

// this prints the data directly from the sheet using tabletop, but does not use the mustache template
function showInfo(data, tabletop) {
var div = document.getElementById('data'),
    html = "<h1>SHEET " + (++count) + "</h1>",
    prop, i;
for(i = 0; i < data.length; i++) {
  for(prop in data[i]) {
    html = html + "&nbsp;-&nbsp;" + data[i][prop];
  }
  html = html + "<hr><br>";
}
div.innerHTML = div.innerHTML + html;
}

我还尝试了使用该脚本打印胡子模板,但是我找不到如何用桌面数据替换{name:“ Luke”}

function loadUser() {
  $.get('template.mst', function(template) {
    var rendered = Mustache.render(template, {name: "Luke"});
    $('#target').html(rendered);
  });
}

0 个答案:

没有答案
相关问题