meteorspacebars

Pass arguments to helpers in Spacebars Template Inclusion


Say I have a template with a template inclusion

<template name="Profile_Page">
    {{> Text_Form_Control value=helper_function}}
</template>

I want to return an array from my helper function and access specific indexes depending on when the helper function is called. However, I cannot simply type helper_function[0] to access the 1st slot in the array. How would one normally access the index of an array if it is passed back from a helper function?


Solution

  • Ideally if a helper returns an array you want spacebars to iterate over it with an {{#each foo}} or {{#Each bar in foo}}

    Otherwise if you want your helper to return different single pieces of data, pass in an argument:

    Template.myTemplate.helpers({
      myHelper(index) {
        var myArray = ['foo', 'bar'];
        return myArray[index];
      }
    });
    

    Template:

    <template name="Profile_Page">
      {{> Text_Form_Control value=(helper_function 0)}}
    </template>