在车把之间注入div元素

时间:2017-03-14 09:33:36

标签: meteor handlebars.js spacebars

我想在#each

的第四个元素之后放置一个div
<script type="text/template" id="template">
{{#each names}}
  {{name}}
{{/each}}
</script>

如果最后没有4个元素。我怎样才能做到这一点?

1 个答案:

答案 0 :(得分:2)

HTML

{{#each names}}
  {{name}}
  {{#if fourth @index count}}
     <div></div>
  {{/if}}
{{/each}}

JS

Template.templateName.helpers(
   {
      count: function(){
           //if you are using iron router to return names
           return Router.current().data().names.find().count();
           // or you could get length from a reactive variable or session 
           // or if you are returning names as a helper, then set the length there
      },
      fourth: function(i, count){
         i = i+1;
         return i%4===0 || count===i;
      }
   }
)