使用Handlebar.js [英] Working with Handlebar.js
本文介绍了使用Handlebar.js的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
但它实际上并没有像预期的那样工作。
我正在做的是我有一个 index.html 文件,
< html xmlns = http://www.w3.org/1999/xhtml\">
< head>
< script src =https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.jstype =text / javascriptcharset =utf-8 >< /脚本>
< script src =http://cloud.github.com/downloads/wycats/handlebars.js/handlebars-1.0.0.beta.6.jstype =text / javascriptcharset = UTF-8\" >< /脚本>
< script src =app.jstype =text / javascriptcharset =utf-8>< / script>
< meta http-equiv =Content-Typecontent =text / html; charset = utf-8/>
< title>梅花与把手< / title>
< / head>
< body>
< script id =ajax-commenttype =text / x-handlebars-template>
< li> $ {
< p> {{comment}}< / p>
< / li>
< / script>
< / body>
< / html>
和一个包含代码的 app.js 。
var source = $(#ajax-comment)。html();
var template = Handlebars.compile(source);
var data = {
name:Jack,
date:12/04/12,
评论:这真是一个很棒的教程,谢谢。
};
$(ul)。append(template(data));
但是我得到的只是一个空白页面。我在这里犯的任何错误?详细解释将有帮助。
解决方案
ul
元素doesn'您必须将其追加到实际存在的内容中。
只需添加< ul>< / ul>
在身体某处。
I was actually trying to find some tutorials on Handlebar.js & I found this http://javascriptplayground.com/blog/2012/05/javascript-templating-handlebars-tutorial
But it is not actually working as expected.
What I am doing is I have an index.html file,
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript" charset="utf-8"></script>
<script src="http://cloud.github.com/downloads/wycats/handlebars.js/handlebars-1.0.0.beta.6.js" type="text/javascript" charset="utf-8"></script>
<script src="app.js" type="text/javascript" charset="utf-8"></script>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Messing with Handlebars</title>
</head>
<body>
<script id="ajax-comment" type="text/x-handlebars-template">
<li>
<span class="meta">{{name}} on {{date}}</span>
<p>{{comment}}</p>
</li>
</script>
</body>
</html>
and an app.js which contains the code.
var source = $("#ajax-comment").html();
var template = Handlebars.compile(source);
var data = {
name : "Jack",
date : "12/04/12",
comment : "This is a really awesome tutorial. Thanks."
};
$("ul").append(template(data));
But all I am getting is a blank page. Any mistake I am making here? Detailed explanations would be helpful.
解决方案
"ul"
element doesn't exist, you must append it to something which actually exists.
just add a <ul></ul>
somewhere in the body.
这篇关于使用Handlebar.js的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文