kwatson
1
I’m trying to store an array in my template that I passed in using a Template helper.
In my template helper, I do this:
Template.someTemplate.helpers({
'someArray': function(){
var array = [ {tic:1,tac:2 }, {tic:3,tac:4}, {tic:5,tac:6}];
return array;
}
});
In my template
< template name=“someTemplate”>
< script> var arr = {{someArray}};
</ template>
but I get a syntax error. I have tried returning a JSON.parse(array) but that also doesn’t work. Any idea what I’m doing wrong here?
1 Like
Didn’t try, but what you are trying to do should return a plain string. So try JSON.stringify(array)
That said, trying to mutate JS in Meteor template seems weird and possibly a wrong use pattern. What are you trying to achieve?
You can only “bind variables” in a template with {{#with}}
, {{#let}}
or {{#each ... in }}
Lets assume we have a {{ log x }}
helper which you can declare as follows
UI.registerHelper('log', s => { console.log(s) })
Then you can write
<template name="someTemplate">
{{#with someArray}}
{{log this}} // this = [1, 2, 3]
{{/with}}
</template>
or
<template name="someTemplate">
{{#let array=someArray}}
{{ log array }} // array = [1, 2, 3]
{{/let}}
</template>
or
<template name="someTemplate">
{{#each element in someArray}}
{{log element}} // element = 1, element = 2, element = 3
{{/each}}
</template>
1 Like