【问题标题】:Using precompiled Handlebars templates in Ember 2 project在 Ember 2 项目中使用预编译的 Handlebars 模板
【发布时间】:2016-07-20 14:11:58
【问题描述】:

我需要在我的 ember 项目中使用一些预编译的车把模板以及我的普通组件和模板。我已经使用app.import() 在我的ember-cli-build.js 文件中加载了预编译的模板。预编译的模板现在显示在 Handlebars.templates 对象中,我在我的 Ember app.js 文件中的应用声明和应用初始化之间映射到 Ember.TEMPLATES["precompiled-template"]

当我检查Ember.TEMPLATES 对象时,预编译模板作为function(context, options) 形式的函数存在,这与其他模板是对象不同。

当我尝试使用预编译模板时,什么也没有出现。有什么想法吗?

编辑1:我说的是小ui模板而不是整个路由模板,我愿意牺牲绑定。

【问题讨论】:

  • 你是如何编译模板的?你是否使用了与 ember.js 相同版本的 ember-template-compiler.js?

标签: javascript ember.js handlebars.js htmlbars


【解决方案1】:

我想出了一种方法来使用帮助器将车把模板转换为字符串并创建文档片段。

import Ember from 'ember';

export function helper(data/*, hash*/) {
  let precompiledTemplateFunction = Handlebars.templates[data[0]];
  return fragmentFromString(precompiledTemplateFunction());
}

function fragmentFromString(strHTML) {
  return document.createRange().createContextualFragment(strHTML);
}

export default Ember.Helper.helper(helper);

用法:{{helper 'precompiledTemplate'}}

我正在将模板导入 ember-cli-build.js 文件 app.import('vendor/path/to/precompiled/template');

我还没有处理通过助手传递属性的问题,但我认为这不会那么难。

【讨论】:

    猜你喜欢
    • 2013-08-19
    • 2012-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-14
    • 1970-01-01
    • 2014-08-29
    相关资源
    最近更新 更多