Search code examples
meteormeteor-blaze

Meteor + Blaze - How can use a condition only in the last element of my loop?


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.


Solution

  • 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>