我正试图在我的Meteor应用程序中的Spacebars模板中编写一个If Else语句,如下所示,但不幸的是我正在处理三个块,即使只有一个返回true,所以想知道是否有人可以请告诉我我在做错了什么/在这里失踪以及如何解决它?感谢
<template name="header">
<nav class="navbar navbar-default navbar-static-top" role="navigation">
<div class="container">
<div class="navbar-header">
<a class="navbar-brand" href="{{pathFor 'home'}}">{{ siteTitle }}</a>
</div>
<div class="collapse navbar-collapse" id="main-nav">
{{#if currentUser}}
<ul class="nav navbar-nav navbar-right">
{{#if isAppAdmin}}
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">
<i class="fa fa-tasks"></i> Admin <i class="fa fa-caret-down"></i>
</a>
</li>
{{else}}
{{#if isProUser}}
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">
<i class="fa fa-tasks"></i> Pro User <i class="fa fa-caret-down"></i>
</a>
</li>
{{/if}}
{{/if}}
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">
<i class="fa fa-user"></i> {{ currentUser.profile.name }} <i class="fa fa-caret-down"></i>
</a>
</li>
</ul>
{{else}}
<div class="navbar-right">
<a href="{{ pathFor 'entrySignIn' }}" class="btn btn-default navbar-btn">Sign in</a>
<a href="{{ pathFor 'entrySignUp' }}" class="btn btn-primary navbar-btn">Register</a>
</div>
{{/if}}
</div>
</div>
</nav>
</template>
header.js
Template.header.helpers({
isProUser: function () {
var retVal = Meteor.user().profile.sholdertype == 'prouser' ? 'true' : 'false';
return retVal;
},
isAppAdmin: function () {
var retVal = Meteor.user().profile.sholdertype == 'admin' ? 'true' : 'false';
return retVal;
}
});
答案 0 :(得分:1)
如果期望布尔值,则返回字符串,更改
Template.header.helpers({
isProUser: function () {
var retVal = Meteor.user().profile.sholdertype == 'prouser' ? 'true' : 'false';
return retVal;
},
isAppAdmin: function () {
var retVal = Meteor.user().profile.sholdertype == 'admin' ? 'true' : 'false';
return retVal;
}
});
要:
Template.header.helpers({
isProUser: function () {
var retVal = Meteor.user().profile.sholdertype == 'prouser' ? true : false;
return retVal;
},
isAppAdmin: function () {
var retVal = Meteor.user().profile.sholdertype == 'admin' ? true : false;
return retVal;
}
});
它应该有用。
答案 1 :(得分:0)
这是你需要的吗?
{{#if isAppAdmin}}
<h2>Admin </h2>
{{else}}
{{#if isProUser}}
<h2>Pro User</h2>
{{else}}
{{#if isRegUser}}
<h2>Regular User </h2>
{{/if}}
{{/if}}
{{/if}}