想象一下,我有一个 Ember 组件,出于这个问题的目的,它只是将其内容包装在 a 中div
,并添加一些文本:
<div class="Component">
Value is: {{yield}}
<div>
所以我称之为
{{#component}}
{{model.text}}
{{/component}}
一切都很好。但是,如果model.text
为空/空/未定义,我根本不想生成该<div class="Component">
部分。我怎样才能做到这一点?我当然可以
{{#if model.text}}
{{#component}}
{{model.text}}
{{/component}}
{{/if}}
但这似乎是重复的。
我真正想做的是能够将组件定义为
{{if yield}} {{! made-up syntax; of course it doesn't work }}
<div class="Component">
Value is: {{yield}}
<div>
{{/if}}
或者,或者在 component.js
yieldNotEmpty: Ember.computed.bool('yield') // made-up syntax; doesn't work
然后在组件的 template.hbs 中
{{if yieldNotEmpty}}
<div class="Component">
Value is: {{yield}}
<div>
{{/if}}
关于如何处理这种情况的任何想法?