简体   繁体   中英

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. I modified Mustache's delimiter but it still isn't working.

.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

template

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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