Reputation: 89
i have a problem, i want to proccess with first element in foreach loop in template as PHP code example:
<?php
$i=0;
foreach($items as $rs){
if($i==0){
echo "first";
}else{
echo "not first";
}
}
?>
Pls help me in template Meteor. Thank you so much
Upvotes: 1
Views: 74
Reputation: 75975
I'm not too sure what you mean without some code of what you want to do, but I think you mean that you want to do something to the first item in a handlebars loop? If its that let me try and give it a shot:
This is a little tricky as the version of handlebars shipped with meteor does not yet do this so you need to do it manually.
Use index values in the transform
Template.hello.items = function() {
var i = 0;
return Items.find({}, { transform: function(doc) {
i++;
if(i==1) doc.first = true;
return doc;
}});
}
So this adds a virtual first
field in your document if its the first one
Your html loop
{{#each items}}
{{#if first}}
This is the first item
{{/if}}
....
{{/each}}
Upvotes: 1