rjmoggach
rjmoggach

Reputation: 1480

Generic session variable key template helper with Meteor

How can I make the following helper more generic so that I can set arbitrary session vars with matching template vars and retrieve them without such a repetitive pattern?

Template.feedback5.helpers({
  'posX': function() {
    return Session.get('posX');
  },
  'dragPosition': function() {
    return Session.get('dragPosition');
  },
  'stuck': function() {
    return Session.get('stuck');
  },
  'dragging': function() {
    return Session.get('dragging');
  }
});

Upvotes: 1

Views: 82

Answers (1)

saimeunt
saimeunt

Reputation: 22696

You can register a global helper to get any Session variable given its key :

Template.registerHelper("getSession",function(key){
  return Session.get(key);
});

And use it like this in your Spacebars templates :

{{getSession "posX"}}

Upvotes: 3

Related Questions