Y declaraciones usando Handlebar

I am wondering if is possible to make something like that using a possible AND statement in handlebar: If not what is the best way to make have the same result.


This is the javascript context:

{
  title: "My first post!",
  author: {
    firstName: "Charles",
    lastName: "Jolley"
  }
}

This is the HTML context:

<div class="entry">
  {{#if firstName && lastName}}
  <h1>{{firstName}} {{lastName}}</h1>
  {{/if}}
</div>

preguntado el 31 de julio de 12 a las 11:07

1 Respuestas

No, the native {{#if}} doesn't understand that syntax. However, you can easily add your own helper for something like that and you can even work with an unknown number of arguments to your helper if you don't mind messing around with arguments.

The only tricky part is noting that the block argument to a helper always comes last.

Something simple like this will do the trick:

Handlebars.registerHelper('if_all_true', function() {
    var args  = Array.prototype.slice.call(arguments);
    var block = args.pop();
    for(var i = 0; i < args.length; ++i)
        if(!args[i])
            return block.inverse(this);
    return block(this);
});

Demostración: http://jsfiddle.net/ambiguous/jMLcZ/

Respondido 31 Jul 12, 17:07

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.