1

I am looping through objects using Handlebars. My issue is that there is no way I can easily add some code every x iterations (% 2 or % 3).

I tried the solution proposed on How can I create conditional row classes using Handlebars.js?. It doesn't work because I am using the framework called Meteor, and my "collection" is coming from a MongoDB query through Meteor. Using that solution, the "Context" is equal to the whole Meteor context and my collection is nowhere to be found...

My first idea was to create a new boolean property on my object called "eof" and a Handlebars condition would be triggered with some more code if it is equal to true... but I would like something cleaner and not dependent of the server-side.

How would you do it?

Thanks a lot!

Community
  • 1
  • 1
TigrouMeow
  • 1,038
  • 4
  • 19
  • 30
  • I tried that one already but I have too many issues... I can't get it to work. The main reason is because I am using Meteor, and the data comes from a Mongo query through Meteor. I will edit the question and add those details. – TigrouMeow Aug 09 '12 at 01:24
  • I am not sure it is the problem, there is already a context = this in the code. Too bad, I don't find a clean way to do it that works, and even the dirty way is quite complicated and doesn't even work yet. Wow, I am used to program everything by myself (I mean, without using any "magic" frameworks), it takes more time, but at least everything is achievable easily. I am starting to worry about the rest of the development. I need a hug from the Meteor team to cheer me up. – TigrouMeow Aug 10 '12 at 01:27
  • Maybe that's going to be hard for you then, I think you need to know how Meteor works a bit, it's not 100% Handlbars translated. I put the last version of my code here https://gist.github.com/3309815, and it seems it works on the development branch of Meteor... so yes, this issue seems very Meteor related :( – TigrouMeow Aug 10 '12 at 05:07

1 Answers1

2

See Tom Coleman's answer.

You could add a template helper that would look something like:

Template.tableWithDifferentColorRows.helpers({
    modulo3: function() {
        return (this.index % 3) === 0;
    },
});
// in the html:
{{#each_with_index collection}}
    <div {{#if modulo3}}style='color:purple'{{/if}}>
         actual content...
    </div>
{{/each}}
Community
  • 1
  • 1
zorlak
  • 1,414
  • 10
  • 15