简体   繁体   English

Django模板中的Mustache.js不起作用

[英]Mustache.js within Django template not working

I want to render ajax response with Mustache.js but for some reason, variables inside templates aren't displayed. 我想用Mustache.js render ajax响应,但由于某种原因,模板内的变量不会显示。 I modified Mustache's delimiter but it still isn't working. 我修改了Mustache的分隔符,但它仍然无法正常工作。

.js .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);
        });

    });

template 模板

<div id="test123"></div>
<script id="reservationPreviewTpl" type="text/template">
<h1>I am a template</h1>
<p><% reservation.updated_at %></p>
</script>

Console output 控制台输出

在此输入图像描述

Turns out reservation.updated_at should just be updated_at 结果是reservation.updated_at应该只是updated_at

template 模板

<div id="test123"></div>
<script id="reservationPreviewTpl" type="text/template">
<h1>I am a template</h1>
<p><% updated_at %></p>
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM