包括外部车把模板?

时间:2013-05-31 09:38:52

标签: templates ember.js handlebars.js template-engine

我正在开发一个玩具EmberJS项目,并且已经到了事情变得非常混乱的地步,所以我正在改进我的文件结构。

我从Discourse's EmberJS application structure获取灵感,将我的控制器,模型,路线等放在单独的文件/文件夹中,但在尝试将我的把手模板包含在我的项目中时遇到了问题。

我正在做的重新包含模板的方法是改变我的index.html:

<script type="text/x-handlebars" data-template-name="students">
    <h2>Students:</h2>
    <ul>
        <li>{{firstName}} - {{lastName}}<li>
    </ul>
</script>

为:

<script type="text/x-handlebars" data-template-name="students" src="js/SD/templates/students.js.handlebars"></script>

但它不起作用。

如何成功包含外部手柄模板文件以用于我的EmberJS应用程序?

1 个答案:

答案 0 :(得分:1)

您更喜欢使用yeoman.io之类的构建工具和相应的ember-generator来自动预编译模板,或者直接为Ember.TEMPLATES数组提供模板,例如:

Ember.TEMPLATES['myTemplate'] = Ember.Handlebars.compile('<p>Hello put in here your template</p>');

因此,在您的情况下,请将此添加到您的app.js文件中:

Ember.TEMPLATES['students'] = Ember.Handlebars.compile('<h2>Students:</h2><ul><li>{{firstName}} - {{lastName}}<li></ul>');

因此可以使用ember进行渲染。

希望有所帮助

相关问题