繁体   English   中英

车把模板未填充上下文

[英]Handlebars template not filling in context

我正在构建一个Flask应用程序,并具有一些客户端HTML渲染,我想使用Handlebars模板来做。

我有以下非常基本的车把模板:

<script id="source-template" type="text/x-handlebars-template">
    <div class="source-data">
        <span>{{last4}}</span>
        <span>Exp : {{exp_month}} / {{exp_year}}</span>
    </div>
</script>

我正在按如下方式使用它(完全在车把教程中使用):

var source = $('#source-template').html();
var template = Handlebars.compile(source);
var context ={
    last4:'1234',
    exp_month:'12',
    exp_year:'2020'
};  
var html = template(context);
console.log(html);

但是,似乎并没有将上下文中的数据插入模板中。 控制台输出为:

<div class="source-data">
    <span></span>
    <span>Exp : / </span>
</div>

我在这里想念什么吗? 我不确定会出什么问题,因为我实质上是复制了handlebars示例

由于我使用的是Flask ,因此脚本是用Jinja渲染的html文件编写的。

因为没有将名为last4exp_month or exp_year传递到Flask的render_template()函数中,所以它什么都没有替换,而把手模板没有变量。

解决方案是在脚本周围使用{% raw %} ,以便Jinja不会将它们解释为变量:

<script id="source-template" type="text/x-handlebars-template">
    {% raw %}
    <div class="source-data">
        <span>{{last4}}</span>
        <span>Exp : {{exp_month}} / {{exp_year}}</span>
    </div>
    {% endraw %}
</script>

暂无
暂无

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

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