Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handlebars - Concat string parameters when calling partial

I would like to know if it is possible to concat a variable with another string when loading a partial using Handlebars.

{{partial logos this ns=../ns nsr=../nsr id=id+"something"}}

I'd like to concat id+"something" and storing it into id, which would be sent to the template.

I'm using a custom helper to load partials (partial) which merge this with the options.hash provided by handlebars.

like image 761
Vadorequest Avatar asked Jul 02 '15 14:07

Vadorequest


People also ask

Does concat work on strings?

The concat() method joins two or more strings. The concat() method does not change the existing strings.

How do I concatenate a substring?

Concatenation is the process of appending one string to the end of another string. You concatenate strings by using the + operator. For string literals and string constants, concatenation occurs at compile time; no run-time concatenation occurs. For string variables, concatenation occurs only at run time.

Does concat return string?

Description. The concat() function concatenates the string arguments to the calling string and returns a new string.


2 Answers

Here's an easier way. A helper named 'concat':

module.exports = function(){
  var arg = Array.prototype.slice.call(arguments,0);
  arg.pop();
  return arg.join('');
};

To be used as:

{{>myPartial id=(concat "foo" myVar myOtherVar)}}
like image 120
Sjeiti Avatar answered Oct 13 '22 01:10

Sjeiti


There is a way actually. I've tried with default partial loader ">", but I hope it should work with "partial" too.

You can write a helper like this

Handlebars.registerHelper( 'concat', function(path) {
    return "/etc/path" + path;
});

and Call it like

{{> responsive-image src=(concat '/img/item-tire.png') alt="logo" }}

I hope that helps.

like image 21
Hasanavi Avatar answered Oct 13 '22 00:10

Hasanavi