Firstly thanks for your help and forgive my infant Meteor and Bootstrap skills. I am having a similar problem to the question raised here that has generate some suggestions but no solution. I want to populate a BootStrap grid using data from MongoDB in a Meteor template using #each. Since the BootStrap grid has 12 columns and I want to display 4 'cells' per row I believe I need to -
- Create a row using .
- Output four data elements inside ...element...
- Close the 'row div' with .
- Create the next row using ...
- Rinse and repeat from Step 2.
Step 2 is performed using a {{#each...}} block returning data from an array/collection.
My Meteor Template looks like this (I'm extending an example from the excellent 'Discovering Meteor' book) -
<template name="postsList">
<div class="posts">
<div class='row-fluid' style="margin-left:1%">
{{breakInit}}
{{#each posts}}
<div class="span3">
{{> postItem}}
</div>
{{breakNow}}
{{/each}}
</div>
</div>
</template>
The JavaScript for the Helpers look like this -
Template.postsList.breakInit = function() {
Template.postsList.docCount = 0 ;
};
Template.postsList.breakNow = function() {
count=Template.postsList.docCount + 1 ;
result="";
if ( count == 4 ) {
count = 0 ;
Template.postsList.docCount = count ;
result="</div><div class=row>" ;
};
Template.postsList.docCount = count ;
return new Handlebars.SafeString(result);
};
This all appears to work, at least in terms of counting the elements output by the #each, returning the </div><div class=row>
to start a new row and resetting the counter... However... the returned HTML to end the current row and start the next does not appear to be handled by Bootstrap (or Meteor or my browser) as I expect it to. It appears to be re-ordered as <div class=row></div>
. See this screen-cap from Inspector in FireFox (code outputs 6 elements, 4 in first row, 2 in second) -
<div id="main" class="row-fluid">
<div class="posts">
<div class="row">
<div class="span3"> … </div>
<div class="span3"> … </div>
<div class="span3"> … </div>
<div class="span3"> … </div>
<div class="row"> … </div> <-- The problem...
<div class="span3"> … </div>
<div class="span3"> … </div>
</div>
</div>
</div>
Notice the <div class=row>...</div>
in the middle of the spans? Doesn't look right, it should be closing the previous 'row' DIV and starting the next one. Can anyone suggest either a fix for my code or an alternative method for popluating a grid using dynamic data in Meteor?