Pass array written in template to meteor/handlebars helper
Asked Answered
D

7

22

I have a helper called printArray that just prints every item in an array. It works great when I define the array in JS and pass it in to the helper via a context object. What I want to do is define the array right in the template, like:

{{printArray arr=[1, 3, 4] }}

Unfortunately, by the time this gets to my helper, the arr key points to undefined. Is there any valid syntax to get the array inside my helper without defining it in javascript?

Davis answered 28/5, 2013 at 3:30 Comment(0)
C
2

You need to use another helper that returns an array

Template.ArrayDemo.helpers({
    arrayValues: [1, 2, 3],
    printArray: function(arr) {
        for (i = 0; i < arr.length; i++) {
            console.log(arr[i]);
        }
    }
 });

now you can do

{{printArray arr=arrayValues}}

Cornerstone answered 12/12, 2015 at 7:17 Comment(0)
G
8

You can use JavaScript's arguments array to accomplish something like this. The arguments array gives you access to every value passed to the function when it is called.

This will allow you to use syntax like this:

{{printArray 1 3 4}}

The code looks like this:

Handlebars.registerHelper('printArray', function() {
    //Last argument is the options object.
    var options = arguments[arguments.length - 1];

    //Skip the last argument.
    for(var i = 0; i < arguments.length - 1; ++i) {
        //Do your thing with each array element.
    }

    //Return your results...
    return '';
});
Goneness answered 16/12, 2014 at 3:21 Comment(0)
F
3

You can almost accomplish this with the use of eval(), using a helper like this:

Handlebars.registerHelper('printArray', function(values) {
   var array = eval(values);

   if (array.constructor === Array()) {
     ...
   }
}

The above allows you to call this from the template:

{{printArray '[0, 1, 2]'}}

The one caveat to this method is that you have to pass your array as a string.

Folium answered 21/9, 2015 at 16:40 Comment(0)
C
2

You need to use another helper that returns an array

Template.ArrayDemo.helpers({
    arrayValues: [1, 2, 3],
    printArray: function(arr) {
        for (i = 0; i < arr.length; i++) {
            console.log(arr[i]);
        }
    }
 });

now you can do

{{printArray arr=arrayValues}}

Cornerstone answered 12/12, 2015 at 7:17 Comment(0)
S
2

Helper:

printArray: (...options) => {
        // remove unneeded properties
        options.pop();
        
        // return option array
        return options;
}

Template:

{{printArray "banana" "apple" "orange" "kiwi"}}

Returns:

["banana", "apple", "orange", "kiwi"]

You can use it in conjunction with other helpers:

{{#each (printArray "banana" "apple" "orange" "kiwi")}}
         {{this}}
{{/each}}
Sideway answered 10/10, 2021 at 16:48 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Hetrick
A
1

Have you tried passing in just the bracketed value of the array?

{{printArray [1, 3, 4]}}

I know you can easily pass in objects, as arguments to the handlebars helper methods:

{{printArray {arr: [1, 3, 4]} }}

Take a look at these awesome helper methods, most of which I stole from elsewhere, a few of which I wrote or tweaked... They are my reference starting point on the topic:

https://github.com/zeroasterisk/Presenteract/blob/master/client/lib/handlebar-helpers.js

Adriatic answered 28/5, 2013 at 5:21 Comment(3)
These look awesome, but I must be misunderstanding something - I get Expecting 'CLOSE', 'DATA', 'STRING', 'INTEGER', 'BOOLEAN', 'ID', 'SEP', got 'INVALID' on the opening curly brace of the object. Could this syntax be allowed in plain Handlebars but disallowed in meteor?Davis
Thanks for the link to your code. Can you point out where you're using these helpers?Davis
Here's an example of getSession github.com/zeroasterisk/Presenteract/blob/master/client/views/…Adriatic
E
1

You can define a array helper as below.

Handlebars.registerHelper('array', function() {
    return Array.prototype.slice.call(arguments, 0, -1);
}

{{printArray (array 1 3 4)}}
Eskridge answered 24/5, 2017 at 3:50 Comment(0)
A
0
{{printArray '1,3,4'}}

Handlebars.registerHelper("printArray", function(values){
    let array = values.split(",");
    console.log(array);
});
Anaplasty answered 2/8, 2021 at 19:20 Comment(1)
Consider adding more explanation about the code, answers should provide more explanation about the code to make the answer more useful and are more likely to attract upvotesBernicebernie

© 2022 - 2024 — McMap. All rights reserved.