A potential problem I see is if you use the template more than once at a time, both instances will be accessing the same variable, to which the solution is to use the template instance:
Template.myTemplate.onCreated(function(){
Template.instance().variable = 1;
});
Template.myTemplate.helpers({
variable: function() {
return Template.instance().variable;
}
});
A slight hacky-feeling approach is to pass a parameter you won’t use to the template:
{{> myTemplate _=""}}
That way the data context will be an object as opposed to the string it is when you use:
{{> myTemplate ""}}
That will make this work
Depending on your use case, you could also do:
{{> myTemplate variable=1}}
And of course, there’s my preferred option, using manuel:viewmodel:
Template.myTemplate.viewmodel({
variable: 1
});
Note that is option is reactive, so it’s actually similar to doing
Template.myTemplate.onCreated(function(){
Template.instance().variable = new ReactiveVar(1);
});
Template.myTemplate.helpers({
variable: function() {
return Template.instance().variable.get();
}
});