Handlebars - command line options when calling partial

I would like to know if it is possible to concatenate a variable with a different line when loading partial use of Handlebars.

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

I would like to concat id+"something" and save it in id , which will be sent to the template.

I use a custom helper to load partial files that combine this with options.hash provided by handles.

+8
javascript
source share
7 answers

There is actually a way. I tried with the incomplete default bootloader ">", but I hope that it will work with "partial" too.

You can write helper helper

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

and name it like

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

I hope this helps.

+7
source share

It’s easier here. Helper named 'concat':

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

Used as:

 {{>myPartial id=(concat "foo" myVar myOtherVar)}} 
+9
source share

You can make a slightly more reusable solution, for example:

 module.exports = function (json) { var concat = ''; var flipArray = []; for(var key in json.hash){ flipArray.push(json.hash[key]); } for(var i = (flipArray.length - 1); i >= 0; i--){ concat += flipArray[i]; } return concat; }; 

Then name it like this:

 {{> icon name=(concat a="file-" b="pdf")}} // passes file-pdf to the partial under the hash value name 

or

 {{concat a="icon-" b="pdf" c="-Asdfasd" d="zxcvzxcvzxcvxz"}} // outputs icon-pdf-Asdfasdzxcvzxcvzxcvxz 

The reason for cycling back in the helper is that the hash parameters of the steering wheel currently list its hash parameters from the last to the first of the order that you declare.

+4
source share

No, It is Immpossible. Use concatenation inside your helper.

 {{partial logos this ns=../ns nsr=../nsr idKey=id idValue="something"}} 
+1
source share

Try the following. Link helper is my own helper to add the / us context path

 Handlebars.registerHelper('link', function(option,parameter) { return '/us' + option.hash.target; }); Handlebars.registerHelper('concat', function() { var outStr = ''; for(var arg in arguments){ if(typeof arguments[arg]!='object'){ outStr += arguments[arg]; } } return outStr; }); 

Then I called it that. My url having puppies

 {{link target=(concat '/' url)}} 

Then finally I got a conclusion similar to this / us / puppies

+1
source share

If you are doing a simple concatenation of a + b and you already include handlebars-helpers , you can use the add helper:

 {{> myPartial myVariable=(add someVariable "some string") }} 
0
source share

In ES6, this is possible with this helper: concat : (...strs) => strs.join('')

You can also skip the options specified by Handlebars, which are: concat : (...strs) => strs.filter( arg => typeof arg !== 'object' ).join('')

0
source share

All Articles