在这篇Using Blaze指南中,Blaze似乎支持{{#if}}和{{else}}语句,但我还没有见过if-else语句的例子。在Blaze中支持这一点吗?或者我必须在else块中做一个额外的if块,这可能会变得很难看。
我尝试过{{else if}},但它给出一个错误。
{{#if en}}{{text.en}}{{else if tc}}{{text.tc}}{{/if}}发布于 2014-12-16 18:54:20
空格键使用与handlebars相同的控制流结构,因此答案与this one相同。在您的案例中:
{{#if en}}
{{text.en}}
{{else}}
{{#if tc}}
{{text.tc}}
{{/if}}
{{/if}}附注-- jade的一个优点是它支持else if。
有时,更好的选择是将逻辑转移到助手中,如下所示:
Template.myTemplate.helpers({
textValue: function() {
if (this.en) {
return this.text.tc;
} else if (this.tc) {
return this.text.tc;
}
}
});<template name="myTemplate">
<p>{{textValue}}</p>
</template>发布于 2016-02-02 02:20:54
在@David Wheldon的精彩回答之后,还值得注意的是,您可以将参数从Blaze模板传递给JavaScript助手函数。
因此,例如,下面的代码通过使用isSelected region customerCompany行调用帮助程序方法来选择性地呈现选择列表的选项
{{#if isSelected region customerCompany}}
<option value={{region._id}} selected>{{region.name}}</option>
{{else}}
<option value={{region._id}}>{{region.name}}</option>
{{/if}}然后在js文件中:
isSelected: function (region, customer) {
return customer.salesRegionId === region._id;
},通常建议使用这种将变量传递给帮助器的方法,以避免在使用模板时因this关键字的含义变化而引起的混乱。
发布于 2018-02-08 15:59:36
当前版本的Blaze支持else if -参见下面的示例格式和对github问题解决方案的参考。
{{#if isUserProfile}}
<h3>User Profile</h3>
{{else if isLawyerProfile}}
<h3>Lawyer Profile</h3>
{{else}}
<h3>Test</h3>
{{/if}}https://stackoverflow.com/questions/27502661
复制相似问题