问题描述
是否可以使用“命名块”?例如,
{{#namedBlock "reusableBlock"}}
{{!-- block with a lot of properties and stuff that will look ugly if you hardcode in multiple places --}}
{{/namedBlock}}
代码中的其他地方
{{#if someCondition}}
<div class="wrapper">
{{reusableBlock}}
</div>
{{else}}
{{reusableBlock}}
{{/if}}
解决方法
我认为您正在寻找组件。 Named blocks是另一回事。
通过在终端中输入ember g component my-component
在Ember CLI中创建组件。或在.hbs
文件夹中手动创建app/components
文件。然后添加一些代码。
{{! my-component.hbs }}
<div class="my-component">
{{! Your code goes here }}
</div>
然后在另一个模板中,调用该组件。
{{! about-page.hbs }}
<h1>This is the about page</h1>
<MyComponent />