简体   繁体   English

Meteor.js中模板助手和模板变量的区别

[英]Difference between Template Helper and Template Variable in Meteor.js

What is the difference between using a Template Helper and a Template Variable (incorrect term?)? 使用模板助手和模板变量(错误的术语?)有什么区别? When do you decide which to use? 你什么时候决定使用哪个?

In the example below, both Template.apple.price function and the quantity function in Template.apple.helpers appear to do the same thing. 在下面的示例中, Template.apple.price功能和quantity的功能Template.apple.helpers似乎做同样的事情。

<template name="apple">
    {{price}}
    {{quantity}}
</template>



Template.apple.price = function() {
    return 20;
}

Template.apple.helpers({
    'quantity': function() {
        return 100;
    }
});

Nothing, as explained in this section of the docs . 没有,正如本文档的这一部分所解释的那样。 The only difference that the second way allows you to use more keywords. 唯一的区别是第二种方式允许您使用更多关键字。 For example, you can't do this: 例如,你不能这样做:

Template.foo.events = function() { /*...*/ };

But you can do this: 但你可以这样做:

Template.foo.helpers({
    "events": function() { /*...*/ }
});

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM