我希望使用Mustache.js render
ajax响应,但由于某种原因,模板内的变量不会显示。我修改了Mustache的分隔符,但它仍然无效。
的.js
/*
* Get reservation preview snippet.
*/
$(document).ready(function() {
Mustache.tags = ['<%', '%>'];
var tableRows = $("#table-wrapper table tr");
// table row was clicked
tableRows.click(function(e) {
e.preventDefault();
var $this = $(this);
var previewURL = $(this).find("#ajax_id").attr('data-id');
// get preview and load it to template
$.getJSON(previewURL, function(reservation) {
console.log("Updated at: " + reservation.updated_at);
var template = $('#reservationPreviewTpl').html();
var html = Mustache.render(template, reservation);
console.log('HTML' + html);
$('#test123').html(html);
});
});
模板
<div id="test123"></div>
<script id="reservationPreviewTpl" type="text/template">
<h1>I am a template</h1>
<p><% reservation.updated_at %></p>
</script>
控制台输出
答案 0 :(得分:0)
原来reservation.updated_at
应该是updated_at
模板
<div id="test123"></div>
<script id="reservationPreviewTpl" type="text/template">
<h1>I am a template</h1>
<p><% updated_at %></p>
</script>