最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to showhide html elements in Handlebars using conditional statement - Stack Overflow

programmeradmin0浏览0评论

I have the following in my handlebars tag:

{{#editmode mode}}

    <div class="form-group login-input">
       <i class="fa fa-key overlay"></i>
       <input type="password" class="form-control text-input" name="password" placeholder="Password" value="{{password}}">
    </div>

    <div class="form-group login-input">
       <i class="fa fa-key overlay"></i>
       <input type="password" class="form-control text-input" name="password_confirmation" value="{{password}}">
     </div>

{{/editmode}}

and I have the following registered as my helper function:

Handlebars.registerHelper('editmode', function(mode){           
    return mode == 'edit' ? true : false;
});

The object passed to the handlebars template looks something like this:

{
   firstname: 'Test',
   lastname: 'Test lastname',
   mode: 'new
}

So basically, whenever the 'mode' variable is 'new', I want to show the password fields, otherwise hide them, but right now they are always hidden. Any ideas?

I have the following in my handlebars tag:

{{#editmode mode}}

    <div class="form-group login-input">
       <i class="fa fa-key overlay"></i>
       <input type="password" class="form-control text-input" name="password" placeholder="Password" value="{{password}}">
    </div>

    <div class="form-group login-input">
       <i class="fa fa-key overlay"></i>
       <input type="password" class="form-control text-input" name="password_confirmation" value="{{password}}">
     </div>

{{/editmode}}

and I have the following registered as my helper function:

Handlebars.registerHelper('editmode', function(mode){           
    return mode == 'edit' ? true : false;
});

The object passed to the handlebars template looks something like this:

{
   firstname: 'Test',
   lastname: 'Test lastname',
   mode: 'new
}

So basically, whenever the 'mode' variable is 'new', I want to show the password fields, otherwise hide them, but right now they are always hidden. Any ideas?

Share Improve this question asked May 20, 2015 at 14:43 BraviBravi 7733 gold badges8 silver badges30 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 3

I figured it out. This did the trick:

Handlebars.registerHelper('editmode', function(mode, options){                      
     return mode == 'edit' ? '' : options.fn();
});

You can use if statement.

<div class="form-group login-input">
   <i class="fa fa-key overlay"></i>
   <input type="password" class="form-control text-input" name="password" placeholder="Password" value="{{password}}">
</div>

{{#if mode}}
<div class="form-group login-input">
   <i class="fa fa-key overlay"></i>
   <input type="password" class="form-control text-input" name="password_confirmation" value="{{password}}">
 </div>
{{/if}}
发布评论

评论列表(0)

  1. 暂无评论