admin管理员组文章数量:1022603
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!!' }}}
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!!' }}}
Share
Improve this question
edited Nov 7, 2014 at 17:49
Deduplicator
45.8k7 gold badges72 silver badges123 bronze badges
asked Jul 1, 2013 at 6:30
Kristoffer KKristoffer K
2,05318 silver badges23 bronze badges
2 Answers
Reset to default 2This 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
Adding the variable plus string as the first argument doesn't seem to work in my limited testing. If it's always going to be a variable and a string that you're passing to the helper, you might as well just add them, even if it seems unnecessary. But leave out the extraneous mas:
{{{ someRandomHelperCreatingALink user.name '/a/cool/url' ' is a cool dude!!' }}}
But, if you may find yourself passing an arbitrary number of options, you could use an options hash:
.js:
Handlebars.registerHelper('createLink', function(options) {
return '<a href="' + options.hash.url + '">' + options.hash.name + '</a>';
});
.html:
{{{ createLink name="Meteor" url="http://meteor." }}}
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!!' }}}
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!!' }}}
Share
Improve this question
edited Nov 7, 2014 at 17:49
Deduplicator
45.8k7 gold badges72 silver badges123 bronze badges
asked Jul 1, 2013 at 6:30
Kristoffer KKristoffer K
2,05318 silver badges23 bronze badges
2 Answers
Reset to default 2This 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
Adding the variable plus string as the first argument doesn't seem to work in my limited testing. If it's always going to be a variable and a string that you're passing to the helper, you might as well just add them, even if it seems unnecessary. But leave out the extraneous mas:
{{{ someRandomHelperCreatingALink user.name '/a/cool/url' ' is a cool dude!!' }}}
But, if you may find yourself passing an arbitrary number of options, you could use an options hash:
.js:
Handlebars.registerHelper('createLink', function(options) {
return '<a href="' + options.hash.url + '">' + options.hash.name + '</a>';
});
.html:
{{{ createLink name="Meteor" url="http://meteor." }}}
本文标签: javascriptPass someVar39a string39 to Handlebarsjs helperStack Overflow
版权声明:本文标题:javascript - Pass someVar+'a string' to Handlebars.js helper? - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745564782a2156382.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论