sir*_*amp 3 javascript handlebars.js ember.js
有谁知道如何在HTML页面中打印.hbs/.handlebars模板?我到处寻找,但我无法弄清楚如何将.hbs文件放在一个目录中,然后在我的index.html中打印它们.很抱歉这个问题,但我是车把的新用户.提前致谢!
假设你有一个把手模板post.handlebars:
<div class="entry">
<h1>{{title}}</h1>
<div class="body">
{{body}}
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
使用把手预编译器来编译模板:
$ handlebars post.handlebars -f templates.js
Run Code Online (Sandbox Code Playgroud)
在html doc中包含已编译的模板和handlebars运行时
<script src="/libs/handlebars.runtime.js"></script>
<script src="templates.js"></script>
Run Code Online (Sandbox Code Playgroud)
现在编译的模板可以作为属性访问Handlebars.templates.接下来将数据传递给模板,生成一些html并将其附加到DOM.
<script type="text/javascript">
var template = Handlebars.templates.post;
var context = {title: "My New Post", body: "This is my first post!"};
var html = template(context);
? $(document).append(html);
</script>
Run Code Online (Sandbox Code Playgroud)
有关预编译的详细信息,请参见http://handlebarsjs.com/precompilation.html.此外还有一个很好的把手教程:http://javascriptissexy.com/handlebars-js-tutorial-learn-everything-about-handlebars-js-javascript-templating/