Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass someVar+'a string' to Handlebars.js helper?

Let's say I have this Handlebars helper:

Handlebars.registerHelper('someRandomHelperCreatingALink', function(passedVarAndString, url) {
    return '<a href="'+url+'">'+passedVarAndString+'</a>';
});

And want to use it like this, where I pass both a string AND a var as the first argument (user.name+' is a cool dude!'):

{{{ someRandomHelperCreatingALink user.name+' is a cool dude!!' '/a/cool/url' }}}

My question: Would that somehow be possible?

Or do I have to add an extra argument for the string (which would feel unnecessary)? Something like this:

Handlebars.registerHelper('someRandomHelperCreatingALink', function(passedVarAndString, url, extraUnnecessary) {
    return '<a href="'+url+'">'+passedVarAndString+extraUnnecessary+'</a>';
});

{{{ someRandomHelperCreatingALink user.name '/a/cool/url' ' is a cool dude!!' }}}
like image 978
Kristoffer K Avatar asked Jul 01 '13 06:07

Kristoffer K


People also ask

What is helper in Handlebars?

Helpers can be used to implement functionality that is not part of the Handlebars language itself. A helper can be registered at runtime via Handlebars. registerHelper , for example in order to uppercase all characters of a string.

How do you concatenate Handlebars?

The {{concat}} helper is designed to concatenate and link multiple things together. The {{concat}} helper will take all of the items passed to it, treat them as strings, and concatenate them together without any spaces. There can be an unlimited amount of items passed to the helper.

How do you compare two values in Handlebars?

{{if}} is used to check whether a field has a value or not, whereas {{compare}} is used to check whether a field has one value or another value. Check out our Handlebars helper reference for more information on handlebars {{compare}}, {{if}}, and other handlebars expressions!

How do I register Handlebars in helpers?

registerHelper("noop", function(options) { return options. fn(this); }); Handlebars always invokes helpers with the current context as this , so you can invoke the block with this to evaluate the block in the current context.


1 Answers

This is not possible because at this point the parameter is just a string. You can either create a second helper to concatenate the strings, either build the string before in a controller

like image 119
CarolineBda Avatar answered Oct 14 '22 01:10

CarolineBda