I'm building a Flask
application and have some client-side HTML rendering which I would like to do using Handlebars
templates.
我正在构建一个Flask应用程序,并且有一些客户端HTML呈现,我想使用Handlebars模板。
I have the following very basic handlebars template:
我有以下非常基本的把手模板:
<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>
And I am using it as follows (exactly as in the handlebars tutorial):
我正在使用它如下(与把手教程完全一样):
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);
However it doesn't seem to be inserting the data from my context into the template. The console output is:
但是它似乎没有将我的上下文中的数据插入到模板中。控制台输出是:
<div class="source-data">
<span></span>
<span>Exp : / </span>
</div>
Am I missing something here? I'm not sure what could be going wrong, as I essentially copied the handlebars example.
我在这里错过了什么吗?我不确定会出现什么问题,因为我基本上复制了把手的例子。
1 个解决方案
#1
2
Since I am using Flask
, I had the script written in an html file which was rendered by Jinja
.
由于我使用的是Flask,我将脚本编写在一个由Jinja呈现的html文件中。
Because there were no variables named last4
, exp_month
, or exp_year
passed into Flask's render_template()
function, it replaced them with nothing, leaving the handlebars template with no variables.
因为没有名为last4,exp_month或exp_year的变量传递给Flask的render_template()函数,所以它没有替换它们,使得把手模板没有变量。
The solution for this was to use {% raw %}
around the script so that Jinja wouldn't interpret these as variables:
解决方法是在脚本周围使用{%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>
#1
2
Since I am using Flask
, I had the script written in an html file which was rendered by Jinja
.
由于我使用的是Flask,我将脚本编写在一个由Jinja呈现的html文件中。
Because there were no variables named last4
, exp_month
, or exp_year
passed into Flask's render_template()
function, it replaced them with nothing, leaving the handlebars template with no variables.
因为没有名为last4,exp_month或exp_year的变量传递给Flask的render_template()函数,所以它没有替换它们,使得把手模板没有变量。
The solution for this was to use {% raw %}
around the script so that Jinja wouldn't interpret these as variables:
解决方法是在脚本周围使用{%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>