从单个辅助函数返回多个不同的值?

时间:2022-02-17 02:00:35

从单个辅助函数返回多个不同的值?

I have a helper compare that returns a css class that simply highlights the text. "better" makes it green, "worse" colors it red. Basically the function compares 2 numbers (the compare function thats commented out does the same as the ternary below it). How can i compare multiple values in the same helper function? I know i could just create a bunch more helper functions and compare all the data 1 by 1, but im sure theres a better way. Heres what the template looks like :

我有一个帮助器比较,它返回一个只突出显示文本的css类。 “更好”使它变绿,“变暗”变成红色。基本上该函数比较2个数字(注释掉的比较函数与它下面的三元组相同)。如何在同一辅助函数中比较多个值?我知道我可以创建一堆更多的辅助函数并逐个比较所有数据,但我确定这是一个更好的方法。这是模板的样子:

从单个辅助函数返回多个不同的值?

2 个解决方案

#1


Return the multiple values as an object from your helper then refer to the keys in your template.

将多个值作为对象从助手返回,然后参考模板中的键。

js:

Template.myTemplate.helpers({
  compare(){
    return { key1: value1, key2: value2, ... keyN: valueN};
  }
});

html:

{{compare.key1}} etc... 

#2


You'd have to pass them as arguments within the function definition itself, something like this should do the trick:

你必须在函数定义本身中将它们作为参数传递,这样的事情可以解决这个问题:

compare: function( number1, number2 ) {
  return number1 > number 2 ? "better" : "worse";
}

#1


Return the multiple values as an object from your helper then refer to the keys in your template.

将多个值作为对象从助手返回,然后参考模板中的键。

js:

Template.myTemplate.helpers({
  compare(){
    return { key1: value1, key2: value2, ... keyN: valueN};
  }
});

html:

{{compare.key1}} etc... 

#2


You'd have to pass them as arguments within the function definition itself, something like this should do the trick:

你必须在函数定义本身中将它们作为参数传递,这样的事情可以解决这个问题:

compare: function( number1, number2 ) {
  return number1 > number 2 ? "better" : "worse";
}