Search code examples
javascripthandlebars.jsexpressionpartials

How can i add expressions to handlebars partials parameters


How can i add expressions to partials parameters? I want to do something like that:

{{> myPartial greeting=(i18n.greeting + "my text") }}

Solution

  • The Handlebars documentation has a section on subexpressions. It tells us that the way to pass the results of inner helper as the argument to outer helpers is done as follows:

    {{> myPartial greeting=(i18n 'greeting') }}
    

    However, it looks from your question that you might be trying to concatenate some string values into a single greeting parameter for your partial. If this is the case, you will need to create (or import) a helper that will concatenate strings for you and then apply this helper as another subexpression. The result would look like the following:

    {{> myPartial greeting=(concat (i18n greeting) 'my text') }}
    

    The required helper could be done as follows:

    Handlebars.registerHelper('concat', function () {
        return Array.prototype.slice.call(arguments, 0, -1).join('');
    });