I'm newbie at meteor and have one big misunderstanding how templates working with data reflection. For example I have some data in database like this:
{name: {firstName: "foo",
lastName: "bar"},
father: "buz"}
the way I can reflect it is really scary for me. I can reflect it only like this:
Js:
var Products = new Meteor.Collection("Products");
if (Meteor.isClient) {
Template.DataTry.dataTryArr = function(){
return DataTry.find({father: "buz"});
};
}
html:
<head><title>...</title></head>
<body>{{>DataTry}}</body>
<template name="DataTry">
{{#each dataTryArr}}
<p>Mr. {{father}} has a son {{name.firstName}}. They are both {{name.lastName}}</p>
{{/each}}
</template>
It is working and reflecting. But I can not understand that {{#each}} and why i need dataTryArr in all! is is not working w/o dataTryArr, {{#if dataTryArr}}, Template.DataTry = function(){...} etc. in all it is not working any way w/o {{#each}} (what to iterate there?!)
Please, help me to understand how to reflect simple data like this way
<head><title>...</title></head>
<body>{{>DataTry}}</body>
<template name="DataTry">
<p>Mr. {{father}} has a son {{name.firstName}}. They are both {{name.lastName}}</p>
</template>
Thanks in advance
You must use either {{#each}} or {{#with}} block helpers to iterate over the values returned from the template helpers.
If your template helper returns bunch of data from the collection, you might need the {{#each}} iterator, in order to render the returned values in DOM. And if the template helper returns single object, in that case {{#with}} block can be used.
And you have suggested this:
<body>{{>DataTry}}</body>
<template name="DataTry">
<p>Mr. {{father}} has a son {{name.firstName}}. They are both {{name.lastName}}</p>
</template>
This will not work in any way, you shuold enclose the <p> ... </p> within {{#each}} block if the Template.DataTry.dataTryArr() returns array of objects.
<template name="DataTry">
{{#each dataTryArr}}
<p>Mr. {{father}} has a son {{name.firstName}}. They are both {{name.lastName}}</p>
{{/each}}
</template>
In simple words, {{#each dataTryArr}} will call the dataTryArr method and will iterate over the values returned.