简体   繁体   中英

Handlebars js if condition

My json part:

var context = {
  author: [{
  id: 47,
  name: "Yehuda Katz"
},
{
  id: 48,
  name: "Kate"
},

{ id: 49, name: "Jim" }

]}

Handlebars

{{if author}}
 <li>{{id}}</li>
 <li>{{name}}</li>
{{/if}}

I want to display the contents once author key is present. If not,it won't display.

However,my above code doen't displays the author eventhough it is present.

Whats wrong in my code??

Handlebars block helpers (like if) need a hash character before the block statment

so instead of

{{if author}}
 <li>{{id}}</li>
 <li>{{name}}</li>
{{/if}}

do

{{#if author}}
 <li>{{id}}</li>
 <li>{{name}}</li>
{{/if}}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM