Can Meteor Templates access Session variables directly?

后端 未结 6 1938
误落风尘
误落风尘 2020-12-08 04:44

In my Meteor app I find myself writing a lot of things like:

Templates.myTemplate1.isCurrentUser = function() {
  return Session.get(\"isCurrentUser\");
};

         


        
6条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-08 05:15

    As meteor is currently using handlebars as default templating engine you could just define a helper for that like:

    if (Meteor.isClient) {
    
    Template.registerHelper('isCurrentUser',function(input){
      return Session.get("isCurrentUser");
    });
    
    }
    

    you can do this in a new file e.g. called helpers.js to keep the app.js file cleaner. Once this helper is registered you can use it in any template by inserting {{isCurrentUser}}

提交回复
热议问题