Steeve Cannon
Steeve Cannon

Reputation: 3682

How can I output the Meteor Session key value pairs in a Handlebars template?

I want to output the Meteor Session key value pairs in a Meteor handlebars template.

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

I have tried returning Session and Session.keys from my Template.name.name js file and then #each in the handlebars to no avail.

I have tried the following to iterate over the Session keys and have tried return an object and it did not work.

I tried the following to iterate over the Session keys and returning a JSON list of data that looks like this. {"myname":"my value","myname2":"myvalue2","myname3":"myvalue"}

_.each(Session.keys, function(element, index, list){

});

Upvotes: 0

Views: 1171

Answers (2)

Steeve Cannon
Steeve Cannon

Reputation: 3682

This is a similar question I asked that has a good solution by 7zark7

Can I subscribe to Meteor Session for reactive template render updates?

Upvotes: 1

Steeve Cannon
Steeve Cannon

Reputation: 3682

This might be an incredibly ugly solution but it works for now. If someone finds a better solution please post your answer. Obviously, you could return it as a JSON or some other kind of format.

Handlebars.registerHelper('getsessions', function() {
    var a = '<div class="span2"><b>name</b></div><div class="span2"><b>value</b></div><br />';
    _.each(Session.keys, function(element, index, list){
        a = a + '<div class="span2">' + index + '</div>' + 
        '<div class="span2">' + element + '</div><div class="span2"></div><br />';
    });
    return a;
  });

Then in the template I put this

{{#getsessions }}{{/getsessions}}

And it displays the Meteor Session contents in HTML.

There is a side effect I have to hunt down or post another SO on. I populate a Meteor Session object on Meteor.startup BUT the above code does not display the contents every time or rather most of the time until the next view render.

S

Upvotes: 0

Related Questions