Can Meteor Templates access Session variables directly?

后端 未结 6 1934
误落风尘
误落风尘 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:35

    Building on @cioddi's answer, as you can pass parameters to the Handlebars helpers, you could make it a generic function so that you can easily retrieve any value dynamically, e.g.

    Template.registerHelper('session',function(input){
        return Session.get(input);
    });
    

    You can then call it in your template like this

    {{session "isCurrentUser"}}
    

    Note that the auth packages come with a global helper named CurrentUser that you can use to detect if the user is logged in:

    {{#if currentUser}}
        ...
    {{/if}}
    

提交回复
热议问题