javascript 把手:将多个参数传递给 registerHelper 函数的最佳方法是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17870635/
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-27 09:55:06  来源:igfitidea点击:

Handlebars : What is the best way to pass multiple parameters to a registerHelper function?

javascripthandlebars.jstemplate-engine

提问by dhruvpatel

I am using Handlebars to render data in a table.

我正在使用 Handlebars 在表格中呈现数据。

One of the data items needs processing, which takes a few parameters into account in order to provide outcome.

其中一个数据项需要处理,需要考虑几个参数才能提供结果。

The templated text example :

模板化文本示例:

{{getOutputByParameters param1=DataFieldName1 param2=DataFieldName2}}

And corresponding registerHelper would be written as:

相应的 registerHelper 将写为:

var __this = this;
Handlebars.registerHelper('getOutputByParameters', function(params){ __this.getOutputByParameters(params.hash.param1, params.hash.param2)})

I figured that handlebars would pass an array of these parameters, which I can access using hash property.

我认为车把会传递这些参数的数组,我可以使用 hash 属性访问这些参数。

But is that the only best way?

但这是唯一最好的方法吗?

回答by user10

Nope. As per their documentyou can do this in many ways. I would do this way.

不。根据他们的文档,您可以通过多种方式执行此操作。我会这样做。

{{getOutputByParameters param1 param2}}

Handlebars.registerHelper('getOutputByParameters', function(param1, param2){ 
//do your stuff here.
})

jsFiddle

js小提琴