javascript Handlebars - 调用部分时的连接字符串参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31187762/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 13:24:47  来源:igfitidea点击:

Handlebars - Concat string parameters when calling partial

javascripthandlebars.js

提问by Vadorequest

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

我想知道在使用 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.

我想连接id+"something"并将其存储到 中id,然后将其发送到模板。

I'm using a custom helper to load partials (partial) which merge thiswith the options.hashprovided by handlebars.

我正在使用自定义帮助程序来加载与把手提供的部分partial合并的部分 ( ) 。thisoptions.hash

采纳答案by Hasanavi

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.

我希望这有帮助。

回答by Sjeiti

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

这是一个更简单的方法。一个名为“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)}}

回答by Mike Mellor

You could do a slightly more reusable solution like so:

你可以做一个稍微更可重用的解决方案,如下所示:

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 call it like so:

然后像这样调用它:

{{> 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 the looping backwards in the helper is because handlebars currently lists it's hash parameters from last to first from the order you declare them.

在 helper 中向后循环的原因是因为 handlebars 当前按照您声明它们的顺序从最后到第一个列出它的哈希参数。

回答by lingeshram

Try following. Link helper is my own helper for adding context path /us

尝试跟随。链接助手是我自己的用于添加上下文路径/us的助手

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 have called like this. My urlhaving puppies

然后我就这样打电话了。我的网址小狗

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

Then finally i got output like this /us/puppies

然后最后我得到了这样的输出/us/puppies

回答by raidendev

No, this is not possible. Use concatenation inside your helper.

不,这是不可能的。在您的助手中使用连接。

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

回答by daleyjem

If you're doing a simple a + bconcatenation and you're already including handlebars-helpers, you can use the addhelper:

如果你在做一个简单的a + b连接并且你已经包含了handlebars-helpers,你可以使用addhelper:

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

回答by Jakub Krol

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

在 ES6 中,这可以使用这个助手: concat : (...strs) => strs.join('')

You may also want to skip parameters given by Handlebars, which is: concat : (...strs) => strs.filter( arg => typeof arg !== 'object' ).join('')

您可能还想跳过 Handlebars 给出的参数,即: concat : (...strs) => strs.filter( arg => typeof arg !== 'object' ).join('')