简体   繁体   English

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

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

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? 但这是唯一最好的方式吗?

Nope. 不。 As per their document you 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 的jsfiddle

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 类型错误:Handlebars.registerHelper 不是函数 - TypeError: Handlebars.registerHelper is not a function 将全局配置变量传递到车把模板的最佳方法是什么? - What is the best way to pass global config variables to handlebars template? 在Node JS应用程序中使用把手时,将TypeError用作registerHelper不是功能 - Getting TypeError as registerHelper is not a function when using handlebars in node js application 带有Ember错误的把手RegisterHelper - Handlebars RegisterHelper with Ember Error 在JavaScript中传递参数以正常运行的正确方法是什么? - What is the correct way to pass parameters to function in JavaScript? 使用 javascript 在 function 中传递多个参数的更简单方法 - Easier way to pass multiple parameters in a function with javascript 在js的as3回调中传递参数的最佳方法是什么 - what is the best way to pass parameters in an as3 callback from js 想要模式以便在JavaScript中将函数参数传递给setTimeout调用的最佳方法 - wanting pattern for best way to pass function parameters into setTimeout call in JavaScript 把手registerHelper链接到HTML文件 - handlebars registerHelper linking to HTML file 在工厂功能中公开参数的最佳方法是什么? - What is the best way to make parameters public in a factory function?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM