【问题标题】:Mustache.js within Django template not workingDjango 模板中的 Mustache.js 不起作用
【发布时间】:2016-05-28 11:44:32
【问题描述】:

我想用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>

控制台输出

【问题讨论】:

  • @andlrc:我已经更新了问题,reservation.updated_at 似乎在工作

标签: javascript django mustache


【解决方案1】:

原来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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-08-10
    • 2013-01-07
    • 1970-01-01
    • 2022-06-10
    • 2011-11-25
    • 2015-02-12
    • 2012-10-15
    • 2023-03-08
    相关资源
    最近更新 更多