2

This is what I have:

Template.publicnewsjson.helpers({ 

news:function(){      

   return news.find({}, { sort: {date:-1} } );

   },

   newscount:function(){

    return news.find().count();
   }
});


  <template name="publicnewsjson">
   <pre>
     {{#each news}}
        {
              Title:{{title}}
              Date:{{friendlydate this.date}}
              Abstract:{{abstract}}
              HeadlineImagePath:{{headlineimagepath}}
              URL:{{url}}
              Source:{{source}}
        }, <------- This is the comma that I want to remove in the last repetition
     {{/each}}
   </pre>
  </template>

How do I make a statement to get the comma in the last repetition? I was trying something like?:

{{#if newscount @index}} but it does not work.

Bugs
  • 4,491
  • 9
  • 32
  • 41
alvespedro
  • 21
  • 4
  • 2
    Add new helper. islast:function(i){ return news.find().count() -1 === i;} – iiro Jul 13 '17 at 17:03
  • @alvespedro this post might help https://stackoverflow.com/questions/21815713/in-meteor-is-there-a-way-to-access-array-index-in-spacebars – Piyush Kumar Jul 13 '17 at 18:34

2 Answers2

0

You can create a helper function, lets call it last, that accepts the @index as a parameter and use {{#unless last @index}},{{/unless}}

Oliver
  • 1,360
  • 9
  • 14
0

Thanks for the help, I was able to solve my problem. That is my new code:

Template.publicnewsjson.helpers({


   news:function(){  

   TAPi18n.subscribe('publicnewslistall', null);  

   return news.find({}, { sort: {date:-1} } );
},

islast:function(position){

 TAPi18n.subscribe('publicnewslistall', null);
 var size = news.find().count();

    if( size === position+1){
      console.log("ultimo");
      return true;
    }
    return false;
   }
});

<template name="publicnewsjson">  

<pre>[{{#each news}}{{#if islast @index}}{
                                      "Title":"{{title}}",
                                      "Date":"{{friendlydate this.date}}",
                                      "Abstract":"{{abstract}}",
                                      "HeadlineImagePath":"{{headlineimagepath}}",
                                      "URL":"{{url}}",
                                      "Source":"{{source}}"
                                }{{else}}

                                {
                                      "Title":"{{title}}",
                                      "Date":"{{friendlydate this.date}}",
                                      "Abstract":"{{abstract}}",
                                      "HeadlineImagePath":"{{headlineimagepath}}",
                                      "URL":"{{url}}",
                                      "Source":"{{source}}"
                                },{{/if}}{{/each}}]</pre>         
</template>
alvespedro
  • 21
  • 4