I have an integer in my mongo collection to define a count, called 'tweet_count'. I need to repeat an icon based on this value, like so:
Helper:
Template.companyList.helpers({
companies: function () {
return Tweets.find();
}
});
Blaze:
{{#each companies}}
{{ LOOP BASED ON tweet_count}}
<i class="fa fa-circle"></i>
{{ /LOOP }}
{{/each}}
A sample of the Mongo collection looks like:
{
"_id" : "xxx",
"name" : "xxx",
"handle" : "xxx",
"tweet_count" : 2,
"tweets" : [
{
"tweet_id" : "x",
"text" : "x",
"created_at" : "Tue Jul 04 15:56:33 +0000 2017",
"retweet_count" : 0,
"from" : "x",
"from_full_name" : "x",
"from_profile_image" : "x"
},
{
"tweet_id" : "x",
"text" : "x9",
"created_at" : "Tue Jul 04 15:56:47 +0000 2017",
"retweet_count" : 0,
"from" : "x",
"from_full_name" : "x",
"from_profile_image" : "x"
}
]
}
How can I achieve this using Meteor/Blaze? I have tried each
but it only accepts an array which I do not have for this value, as it's simply a number.
Just create a helper that returns an array with the size of tweet_count:
Template.companyList.helpers({
companies: function () {
return Tweets.find();
},
getTweetCount(tweet_count) {
const ret = [];
for (let i = 0; i < tweet_count; i++) {
ret.push(i);
}
return ret;
}
});
It receives the tweet count and returns the empty array with size of tweet count. You call it from Blaze Syntax in the following way:
<template name="companyList">
{{#each companies}}
<span>{{this.name}}</span>
{{#each getTweetCount this.tweet_count}}
<i class="fa fa-circle"></i>
{{/each}}
{{/each}}
</template>
The this
refers to the current document, so this.tweet_count is the number that gets passed to your helper function getTweetCount.