SORU
13 Ocak 2012, Cuma


Bir handlebars.js mantıksal işleç {{#if}} koşullu

Gidon standart handlebars.js koşullu mantıksal operatörler operatör içine dahil JS bir yolu var mı? Şöyle bir şey:

{{#if section1 || section2}}
.. content
{{/if}}

Benim kendi Yardımcısı yazabilirim biliyorum, ama ilk tekerleği yeniden icat değilim emin olmak istiyorum.

CEVAP
1 Mayıs 2013, ÇARŞAMBA


Bir adım daha ileri alarak çözüm. Bu operatör karşılaştırın ekler.

Handlebars.registerHelper('ifCond', function (v1, operator, v2, options) {

    switch (operator) {
        case '==':
            return (v1 == v2) ? options.fn(this) : options.inverse(this);
        case '===':
            return (v1 === v2) ? options.fn(this) : options.inverse(this);
        case '<':
            return (v1 < v2) ? options.fn(this) : options.inverse(this);
        case '<=':
            return (v1 <= v2) ? options.fn(this) : options.inverse(this);
        case '>':
            return (v1 > v2) ? options.fn(this) : options.inverse(this);
        case '>=':
            return (v1 >= v2) ? options.fn(this) : options.inverse(this);
        case '&&':
            return (v1 && v2) ? options.fn(this) : options.inverse(this);
        case '||':
            return (v1 || v2) ? options.fn(this) : options.inverse(this);
        default:
            return options.inverse(this);
    }
});

Böyle bir şablon olarak kullanın

{{#ifCond var1 '==' var2}}

Kahve Script Sürümü

Handlebars.registerHelper 'ifCond', (v1, operator, v2, options)->
    switch operator
        when '==', '==='
            return if v1 is v2 then options.fn this else options.inverse this
        when '<'
            return if v1 < v2 then options.fn this else options.inverse this
        when '<='
            return if v1 <= v2 then options.fn this else options.inverse this
        when '>'
            return if v1 > v2 then options.fn this else options.inverse this
        when '>='
            return if v1 >= v2 then options.fn this else options.inverse this
        when '&&'
            return if v1 && v2 then options.fn this else options.inverse this
        when '||'
            return if v1 || v2 then options.fn this else options.inverse this
        else
            return options.inverse this

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • Friday NightFort

    Friday Night

    15 EYLÜL 2011
  • ModNation Racers H.Q.

    ModNation Ra

    31 Ocak 2010
  • sghaff1

    sghaff1

    23 Mart 2009