1

Typically in Meteor templates, if you supply data through a router like iron-router, you are returning a set of documents (really a cursor) that you iterate through with the #each operator. But what if you only want to return a specific element from some field that is an array? I've tried several things, including the $slice operator in my query, but nothing seems to work.

6
  • Are you asking something like this: "I want to pass an array from iron-router as data and show element n"? Or are you asking something about loading data from the DB and showing only a single field? I can't really tell without code or more explanation. Commented Jan 21, 2014 at 22:40
  • Yes, I want to pass an array from iron-router and show element n. It's actually a sub-document: 'key: uniqueIdentifier, {myData: [ {field1: field1Value1, field2: field2Value1}, {field1: field1Value2, field2: field2Value2} ]' ... and say I want to return the second element of myData. Or I could return the whole document, if I just knew how to drill down to the desired array element in the template. Commented Jan 21, 2014 at 22:57
  • Okay see if the example I gave in my answer helps. You probably need something like: return this['myData'][1]; Commented Jan 21, 2014 at 23:26
  • One trick that may help: just add a console.log(this) to the first line of your helper. That's a quick way to see the structure you need to access. Commented Jan 22, 2014 at 0:31
  • Well, that was a good idea I hadn't thought of. But it showed me the structure I expected to see -- still can't figure out how to access that array element. Here's the more detailed version: link Commented Jan 22, 2014 at 1:18

1 Answer 1

4

Here is a simple route that sets data to an array:

Router.map(function() {
  this.route('home', {
    path: '/',
    template: 'home',
    data: [{fruit: 'apples', count: 10}, {fruit: 'oranges', count: 5}]
  });
});

The home template looks like this:

<template name="home">
  <p>{{snack.fruit}}: {{snack.count}}</p>
</template>

To show 'apples' as the snack, you need only access the first element of this:

Template.home.helpers({
  snack: function() {
    return this[0];
  }
});

Note, however, that this inside of the helpers is not actually an array - it will be an object with the same key-value pairs as the data you supplied (as well as a yield function apparently).

Sign up to request clarification or add additional context in comments.

3 Comments

Great. That would have taken me some time to figure out. I should be able to make this work. Just out of curiosity, is it possible pass a parameter to that template helper in a case like this? Would {{snack.fruit parameter}} work?
No you can't pass parameters to your template helpers. You can pass parameters to a handlebars helper, e.g. {{formatSnackCount snack.count}} where formatSnackCount has been registered with Handlebars.registerHelper.
Thanks for this! Another nugget of clarity while learning the basics of Meteor :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.