26

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

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


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

I need many different templates (I'm using handlebars) to access the same simple value stored inside Session.

Is there a way to avoid writing the same function over and over again? Thanks

George Strakhov
  • 616
  • 2
  • 7
  • 10

6 Answers6

46

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}}
Community
  • 1
  • 1
WispyCloud
  • 4,140
  • 1
  • 28
  • 31
  • @jtblin Hey in this approach, if the variable returned is an array, can you get the value in them? Like `{{session "isCurrentUser".1}}` (which doesn't work) ? – Mercutionario Mar 03 '13 at 17:38
  • In this case you may have to create a separate method, and pass the index of the array that you want to get back in a second variable, i.e. `Handlebars.registerHelper('sessionArray',function(input, index){ return Session.get(input)[index]; });` and call it like this `{{sessionArray "isCurrentUser" 1}}`. – WispyCloud Mar 03 '13 at 21:41
  • [I am facing an issue](https://stackoverflow.com/questions/32806020/spacebar-meteor-nested-if-and-global-template-helpers) using this technique with nested `{{#if}}` tags – Alexandre Bourlier Sep 27 '15 at 08:35
27

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

Dan Dascalescu
  • 143,271
  • 52
  • 317
  • 404
cioddi
  • 763
  • 4
  • 12
  • 3
    Thanks for the hint, the `Handlebars.registerHelper` method is of course a good place to add globally used template elements. – Andreas Oct 26 '12 at 17:38
  • 1
    I wish I saw this earlier, I'm falling back to jQuery just to add global events instead of template events – TiansHUo Nov 20 '12 at 10:21
  • 2
    The `Handlebars` method of registering helpers was deprecated in `0.8.0`. `UI.registerHelper` is the new method of doing this. – mjkaufer Apr 09 '14 at 18:39
12

Just a heads up to everyone: With the release of 0.8.0, Handlebars.registerHelper has become deprecated. Using the new Blaze engine, UI.registerHelper would be the new method of accomplishing this.

Updated version of @cioddi 's code

UI.registerHelper('isCurrentUser',function(input){
  return Session.get("isCurrentUser");
});
mjkaufer
  • 4,047
  • 5
  • 27
  • 55
5

Actually now you can just use {{#if currentUser}}

It's a global included from the accounts/auth package..

http://docs.meteor.com/#template_currentuser

George Katsanos
  • 13,524
  • 16
  • 62
  • 98
5

You'll want to check out these handlebar helpers for meteor: https://github.com/raix/Meteor-handlebar-helpers

There's a number of session helpers, one which does what you want. From the docs:

Is my session equal to 4?: {{$.Session.equals 'mySession' 4}}

Nick Budden
  • 621
  • 9
  • 20
1

You could add a isCurrentUserTemplate and include this in your other templates with

{{> isCurrentUserTemplate}}
Andreas
  • 1,622
  • 14
  • 13