简体   繁体   中英

Passing multiple parameters between templates in meteor

I have a parent and a child template. I want to pass the data context and couple of other parameters from parent to child. So far the below code is not working

<template name='parent'>
  {{#each arrayFromHelper}}
     {{> child this groupName='biggroup'}}
  {{/each}} 
</template>

<template name='child'>
 {{this}}
 {{groupName}} 
</template>

i am not being able to access the groupName in child template.

You have a two options:

rename the context and add more variables

{{> child data=this groupName='biggroup'}}

Now child has access to both the parent context and the groupName . The only trick is that you'll need to access the context via the data namespace (or whatever you choose to call it) which may seem a little verbose.

<template name='child'>
 {{data.someValue}}
 {{groupName}}
</template>

extend the context in a helper

Add a helper to the parent template which extends its context like this:

Template.parentTemplate.helpers({
  context: function() {
    var result = _.clone(this);
    result.groupName = 'biggroup';
    return result;
  }
});

Then in your template you can do:

{{> child context}}

The child template will then have the parent context along with the groupName . This is the technique I prefer whenever possible.

Also see my answer to this question if you'd prefer to do this with a global helper.

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