Warning: this post is old and might not reflect the current state of the art

Here’s a simple example on combining Session variables and template helpers to achieve a simple case of selecting the current comment selected in a list.

In our template.html file:

<template name="main">
  {{#each comments}}
    {{> comment}}
  {{/each}}
</template>

<template name="comment">
  <div class="{{#if selected}}selected{{/if}}">
    <p>{{this.comment}}</p>
  </div>
</template>

In template.js:

Session.set('selectedComment', null);

Template.comments.helpers({
  comments: function() {
    return Comments.find();
  }
});

Template.comment.helpers({
  selected: function() {
    return Session.equals('selectedComment', this._id);
  }
});

Template.comment.events({
  'click div' : function(e, t) {
    Session.set('selectedComment', this._id);
  }
});

In this case any time I click a comment, that comment becomes the selected comment, and we can show it full-size, fetch the other comments made by the user or do some other fancy stuff.

Want to hire me?

I'm currently considering remote job/contract opportunities as a Frontend Developer.

Read more about me and if you're interested, get in touch.