user1447679
user1447679

Reputation: 3230

Meteor - Using session in handlebars conditions?

I have a user profile page that has its own nav items (I.e. About me, Pictures, etc.) I've click functions on these to set a session variable.

In my template, I want to show content based on the session ( Unless there's a better way )

Example:

 {{#if activeTabIs "About Me"}}
     {{> ProfileAbout}}
 {{/if}}
 {{#if activeTabIs "Pictures"}}
     {{> ProfileGallery }}
 {{/if}}

Template: userProfile

...

// I've tried this
Template.userProfile.activeTabIs = function() {
    return Session.get('ActiveProfileTab');
};

// And this
Handlebars.registerHelper('activeTabIs', function(){
    return Session.get("ActiveProfileTab");
});

Manager

The above always returns true and shows everything. If I just use a helper like:

{{activeTabIs}}

It works, as in when I switch the session around via console it changes dynamically. But the handlebars if condition doesn't work.

If there's a better way to handle this I'd love some thoughts. For example, if I can just have:

{{RenderActiveTab}}

However I'd still like to understand what I'm doing wrong in the conditions. I've referenced the docs in the league example, however it wasn't using a session.

Upvotes: 1

Views: 1092

Answers (2)

GeriTol
GeriTol

Reputation: 174

I think you could do this with a router. (e.g. iron-router)

this is the code for changing templates when the user clicks the link:

Router.map(function() {
    this.route('home', {path: '/'});
    this.route('ProfileAbout', {path: '/about'});
    this.route('ProfileGallery', {path: '/gallery'});

    });

 //html example

 <a href="/about">This shows the ProfileAbout template</a>

More on how to use Iron Router: https://github.com/EventedMind/iron-router

//you can do some crazy stuff with it

Upvotes: 0

David Weldon
David Weldon

Reputation: 64312

It seems like this should work as a global helper:

Handlebars.registerHelper('activeTabIs', function(tab){
  return Session.equals('ActiveProfileTab',  tab);
});

Then you can use it in your templates like in your initial example. Give that a try and let me know if it works.

Upvotes: 0

Related Questions