把手示例不起作用 [英] Handlebars example not working

查看:67
本文介绍了把手示例不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的节点服务器提供的.hbs中:

In my .hbs delivered by my node server:

<script src="/javascripts/handlebars.min-latest.js"></script>

<script id="entry-template" type="text/x-handlebars-template">
  <div class="entry">
    <h3>{{title}}</h3>
    <div class="body">
      {{body}}
    </div>
  </div>
</script>

在我的客户端javascript文件中:

In my client-side javascript file:

var source   = $("#entry-template").html();
var template = Handlebars.compile(source);
var context = {title: "My New Post", body: "This is my first post!"};
var html    = template(context);
console.log(html);

我的输出:

<div class="entry">
    <h3></h3>
    <div class="body">

    </div>
</div>

基本上,即使首页上有最简单的示例,也无法正常工作.是因为我在前端和后端使用了把手?如果是这样,我该如何解决?

Basically, even with the simplest example on their frontpage, it isn't working. Is it because I'm using handlebars on the front and back end? If so, how can I go around that?

推荐答案

如果对后端和前端都使用了Handlebars,则很容易解决.只需通过在 \ {{myVar}} 的前面添加一个 \ 即可逃避用于前端解析器的变量,然后保留要保留的变量.服务器解析为 {{myServerVar}} .

If you use Handlebars for both backend and frontend there is an easy fix. Just escape the variables which are meant for your frontend parser by adding a \ in foront of them like so \{{myVar}}, and leave the variables which are to be parsed by the server to {{myServerVar}}.

<script type="text/x-handlebars-template" id="my-row-template"> ​
    <tr>
        <td></td>
        <td>\{{fieldName}}</td>
    </tr>
</script>

这篇关于把手示例不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆