Handlebarsjs check if a string is equal to a value

后端 未结 13 762
挽巷
挽巷 2020-12-08 18:23

Is it possible in Handlebars to check if a string is equal to another value without registering a helper? I can\'t seem to find anything relevant to this in the Handlebars r

相关标签:
13条回答
  • 2020-12-08 19:16

    It seems you can't do it "directly"

    Try use helper, why not?

    Register helper in your javascript code:

    Handlebars.registerHelper('ifEquals', function(arg1, arg2, options) {
        return (arg1 == arg2) ? options.fn(this) : options.inverse(this);
    });
    

    Use in template:

    {{#ifEquals sampleString "This is a string"}}
        Your HTML here
    {{/ifEquals}}
    

    More details here: Logical operator in a handlebars.js {{#if}} conditional

    Update: Another way:

    lets assume, your data is:

    var data = {
        sampleString: 'This is a string'
    };
    

    Then (using jQuery):

    $.extend(data, {isSampleString: function() {
        return this.sampleString ==  'This is a string';}
    });
    

    An use template:

    {{#if isSampleString}}
        Your HTML here
    {{/if}}
    
    0 讨论(0)
  • 2020-12-08 19:16

    Use handlebars-helpers, which provides eq helper

    0 讨论(0)
  • 2020-12-08 19:18

    The Mandrill mail service supports Handlebars and here it is possible to use "backticks" to evaluate an logical expression in a #if block:

    {{#if `operating_system == "OS X"`}}
      <p>Click here for instructions to install on a Mac</p>
    {{elseif `operating_system == "Windows"`}}
      <p>Click here for instructions to install on a PC</p>
    {{/if}}
    

    I don't know if this possible in general, but you should try it out. It works fine for me.

    0 讨论(0)
  • 2020-12-08 19:23

    I would just use helpers like this:

    Handlebars.registerHelper('ifeq', function (a, b, options) {
        if (a == b) { return options.fn(this); }
        return options.inverse(this);
    });
    
    Handlebars.registerHelper('ifnoteq', function (a, b, options) {
        if (a != b) { return options.fn(this); }
        return options.inverse(this);
    });
    

    Then in your code:

    {{#ifeq variable "string"}} 
        ... do this ... 
    {{/ifeq}}
    {{#ifnoteq variable "string"}} 
        ... do this ... 
    {{/ifnoteq}}
    
    0 讨论(0)
  • 2020-12-08 19:25

    Node.js add this in server.js

    const isEqual = function(a, b, opts) {
      if (a == b) {
        return opts.fn(this) 
      } else { 
        return opts.inverse(this) 
      } 
    }
    
    var hbs = require('hbs');
    hbs.registerHelper('if_eq', isEqual);
    
    0 讨论(0)
  • 2020-12-08 19:27

    A common case for a simple, re-usable helper function is to cause a return of string1 if values are equal and string2 if they are not.

    Example:

    Helper (let's call it "ifEqual" and send 4 parameters):

    helpers: {
    
        ifEqual: function (obj, value, trueString, falseString) {
                return ( (obj===value) ? trueString : falseString );
    }
    

    Template Use:

    For this example, assume the template receives a "transaction" object with a "transactionType" property: { transactionType: "expense", description: "Copies" }

    Let's say our template has a <select> for the Transaction Type, with various <option>s as shown. We want to use Handlebars to pre-select the option which coincides with the value of transactionType.

    Our new {{ ifEqual }} helper is used to insert "selected" for the <option> with the matching value of "expense."

    <select required id="selTransactionType" name="selTransactionType" class="form-control" onchange='transactionTypeChanged()'>
       <option value='hourly' {{ ifEqual transaction.transactionType "hourly" "selected" "" }} >Hourly fee</option>
       <option value='flat' {{ ifEqual transaction.transactionType "flat" "selected" "" }} >Flat fee</option>
       <option value='expense' {{ ifEqual transaction.transactionType "expense" "selected" "" }} >Expense</option>
       <option value='payment' {{ ifEqual transaction.transactionType "payment" "selected" "" }} >Payment</option>
       <option value='credit' {{ ifEqual transaction.transactionType "credit" "selected" "" }} >Credit</option>
       <option value='debit' {{ ifEqual transaction.transactionType "debit" "selected" "" }} >Debit</option>
    </select>
    
    0 讨论(0)
提交回复
热议问题