我正在使用 Handlebars.js 进行客户端视图渲染。如果 Else 效果很好,但我遇到了需要 ELSE IF 的 3 路条件:
这不起作用:
{{#if FriendStatus.IsFriend }} <div class="ui-state-default ui-corner-all" title=".ui-icon-mail-closed"><span class="ui-icon ui-icon-mail-closed"></span></div> {{else if FriendStatus.FriendRequested}} <div class="ui-state-default ui-corner-all" title=".ui-icon-check"><span class="ui-icon ui-icon-check"></span></div> {{else}} <div class="ui-state-default ui-corner-all" title=".ui-icon-plusthick"><span class="ui-icon ui-icon-plusthick"></span></div> {{/if}}
我如何用车把做 ELSE IF?
自 3.0.0 起, Handlebars 支持{{else if}}块。
{{else if}}
车把 v3.0.0 或更高版本:
{{#if FriendStatus.IsFriend}} <div class="ui-state-default ui-corner-all" title=".ui-icon-mail-closed"><span class="ui-icon ui-icon-mail-closed"></span></div> {{else if FriendStatus.FriendRequested}} <div class="ui-state-default ui-corner-all" title=".ui-icon-check"><span class="ui-icon ui-icon-check"></span></div> {{else}} <div class="ui-state-default ui-corner-all" title=".ui-icon-plusthick"><span class="ui-icon ui-icon-plusthick"></span></div> {{/if}}
但是,在 Handlebars v3.0.0 之前,您必须定义一个处理分支逻辑的助手或if手动嵌套语句:
if
{{#if FriendStatus.IsFriend}} <div class="ui-state-default ui-corner-all" title=".ui-icon-mail-closed"><span class="ui-icon ui-icon-mail-closed"></span></div> {{else}} {{#if FriendStatus.FriendRequested}} <div class="ui-state-default ui-corner-all" title=".ui-icon-check"><span class="ui-icon ui-icon-check"></span></div> {{else}} <div class="ui-state-default ui-corner-all" title=".ui-icon-plusthick"><span class="ui-icon ui-icon-plusthick"></span></div> {{/if}} {{/if}}