简体   繁体   中英

Return Multiple Values in Meteor for Handlebars Template

How do I do this in Meteor?

Template.foo.bar = function() {


someVar = return value of some other function
return SomeCollection and someVar;

}

-------Template----

{{#each someCollection}}
{{someVar}} {{someCollectionField}}
{{/each}}

in regular javascript I could just use an array to return multiple values how does it work in Meteor?

You could return a js object and use handlebars to go through it

Client js

Template.foo.bar = function() {
    someVar = getmyotherfunction();
    return {
            SomeCollection: SomeCollection.find({...}),
            someVar: someVar
           };

}

Client html

<template name="foo">
    {{#each bar.SomeCollection}}
        {{bar.someVar}} 
        {{someCollectionField}}
    {{/each}}
</template>

You can access a bar value inside the handlebars each loop and just use . to get inside objects. Arrays also work, use .0 to get the first item in the array, 0 being the index.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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