Ramesh Murugesan
Ramesh Murugesan

Reputation: 5013

Meteor : How to get iron-router parameter in template

How to get get route param values in template?

Router

Router.map(function() {
  this.route('userpost', {path: '/mypost/:_id'});
  this.route('usercomment', {path: '/mycomments/:_id'});
});

my current location is localhost:3000/mypost/12345. I want to assign a path parameter from a route param

Template

<template name="mytemplate">
    <a class="tab-item" href="{{pathFor 'userpost' _id=???}}">Post</a>
    <a class="tab-item" href="{{pathFor 'usercomment' _id=???}}">Comment</a>
</template>

Upvotes: 4

Views: 1937

Answers (1)

saimeunt
saimeunt

Reputation: 22696

{{pathFor}} is using the current data context to replace URL parameters with actual values, so you need to enclose the call inside a {{#with}} block helper.

<template name="mytemplate">
  {{#with context}}
    <a class="tab-item" href="{{pathFor "userpost"}}">Post</a>
    <a class="tab-item" href="{{pathFor "usercomment"}}">Comment</a>
  {{/with}}
</template>

context is a helper returning an object that have an _id, and this property will be used to fill-in the computed path.

Template.mytemplate.helpers({
  context: function(){
    return {
      _id: Router.current().params._id
    };
  }
});

Upvotes: 5

Related Questions