简体   繁体   中英

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');
  }
});

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"}}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM