Question

How can i use if / else in hogan. I have following code. Status it is enum, which has two value("locked", "unlocked").I need to check, if status=="locked" then used class="label label-danger" , else class="label label-success". The problem in the syntax, how to do it.

template: [
           '<p class="repo-language"><span class="glyphicon glyphicon-user">' +
           '</span> <strong>{{fullName}}</strong><span class="pull-right label label-danger">{{status}}</span></p>',
           '<p class="repo-name"><small>{{login}} / {{email}}</small></p>'
           ].join(''),
           engine: Hogan
Was it helpful?

Solution

I don't know if it's a good practice, but functions are evaluated, I'm doing conditions like that:

user = {
  name: 'foo',
  status: 'locked',
  statusClass: function() {
    if (this.status == 'locked')
      return 'danger'
    else
      return 'success'
  }
}

var template = Hogan.compile('<span class="label label-{{user.statusClass}}">{{user.status}}</span>')
alert(template.render({user: user}));

http://jsfiddle.net/WL4QC/

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top