Javascript 在 html 中包含 .handlebars 或 .hbs 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25758144/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
include a .handlebars or a .hbs file in html
提问by Millzie
At the homepage of emberjs.com there is a emberjs & handlebarsjs example of a todo list. On the todo list there is a extension for a .hbs file and I was wondering what is .hbs? And how do I include a .hbs script to HTML? Something like this:
在 emberjs.com 的主页上有一个待办事项列表的 emberjs & handlebarsjs 示例。在待办事项列表中有一个 .hbs 文件的扩展名,我想知道什么是 .hbs?以及如何将 .hbs 脚本包含到 HTML 中?像这样的东西:
<script type="text/hbs" src="hbs-file.hbs"></script>
回答by jfriend00
It's a handlebars template file which is a piece of HTML with handlebars tokens in it. It doesn't matter what the file extension is; it can be anything you want.
它是一个把手模板文件,它是一段 HTML,其中包含把手标记。文件扩展名是什么并不重要;它可以是你想要的任何东西。
To include a handlebars template in your own HTML, you just create the file, give it any name you like, and then include a <script>tag like the one in the example that points to your template file.
要在您自己的 HTML 中包含一个把手模板,您只需创建该文件,为其指定任何您喜欢的名称,然后包含一个<script>类似于示例中指向您的模板文件的标记。
One way of using client-side handlebars templates is to include them in script tags (as in your example). The raw template will be available in the DOM, but not made visible and not processed as HTML so it it already available to your code so it can be compiled into a template by your client javascript and then rendered to HTML with a specific set of data.
使用客户端把手模板的一种方法是将它们包含在脚本标记中(如您的示例中所示)。原始模板将在 DOM 中可用,但不可见,也不作为 HTML 处理,因此它已经可用于您的代码,因此它可以由您的客户端 javascript 编译成模板,然后使用一组特定的数据呈现为 HTML .
Handlebars templates can also be in your javascript, built by your javascript, or can be loaded dynamically via Ajax (two other ways of getting them into the client in addition to the <script>tag method).
Handlebars 模板也可以在您的 javascript 中,由您的 javascript 构建,或者可以通过 Ajax 动态加载(除了<script>tag 方法之外,还有两种将它们放入客户端的方法)。
If you were using handlebars server-side, then the templates can stay on the server and don't need to be put in the page as <script>tags.
如果您在服务器端使用把手,那么模板可以保留在服务器上,不需要作为<script>标签放在页面中。

