如何使用Ember处理指令行生成的模板? [英] How can I consume handlebars command-line generated templates with Ember?
问题描述
我使用预编译的模板有几个原因:
I'm using precompiled templates for several reasons:
- 性能(无需在运行时重新编译)
- 代码分离(比嵌入
< script>
代码和硬编码在JS) - 内容安全策略(这是一个扩展名)。
- Performance (no need to re-compile at runtime)
- Code separation (cleaner than embedding
<script>
tags and hardcoding in JS) - Content security policy (this is for an extension).
基本上,我正在生成 templates.js
通过句柄命令行实用程序,基于几个 template.handlebars
文件。接下来,我尝试通过以下循环将这些模板带入Ember:
Basically, I'm generating templates.js
via the handlebars command line utility, based on several template.handlebars
files. Next I try to bring these templates into Ember with the following loop:
for (var name in Handlebars.templates) {
var template = Handlebars.templates[name];
Ember.TEMPLATES[name] = template;
}
结果很奇怪:文本似乎加载,但很多模板功能例如, {{outlet}}
)不工作。我怀疑这是因为Handlebars和Ember-Handlebars是不一样的。
The result is weird: text seems to be loaded, but many template features (eg. {{outlet}}
) don't work. I suspect that this is because Handlebars and Ember-Handlebars are not the same thing.
我想有两个选项(和问题):
I guess there are two options (and questions):
- 预编译为Ember友好的模板(如何通过命令行执行此操作?)
- 将Handlebars模板正确导入到Ember如何?)
更新:根据答案,Ember.Handlebars与Handlebars不一样,所以预编译是不同的。写了一个简单的脚本来为Ember预编译: https://gist.github.com/3723927
UPDATE: As per the answer, Ember.Handlebars is not the same as Handlebars, so the precompilation is different. Wrote a simple script to precompile for Ember: https://gist.github.com/3723927
推荐答案
是的,普通Handlebars编译器编译为不同于Ember.Handlebars的JavaScript,因此您不能使用Ember消耗其输出。
Yes, the plain Handlebars compiler compiles to different JavaScript than Ember.Handlebars, so you cannot consume its output with Ember.
我不知道通过命令行运行Ember.Handlebars的方式,但原则上应该可以写一些东西。
I don't know of a way to run Ember.Handlebars through the command line, though it should be possible in principle to write something.
要了解如何使用Ember.Handlebars进行预编译,请查看 ember的源代码-rails - 它支持预编译。
To find out how to precompile with Ember.Handlebars, take a look at the source code of ember-rails - it supports precompilation.
这篇关于如何使用Ember处理指令行生成的模板?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!