Meteor: how to get the "iron router" parameter in a template

How to get the values โ€‹โ€‹of route parameters of a route in a template?

router

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

      

my current location localhost:3000/mypost/12345

. I want to assign a path parameter from a route parameter

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>

      

+3


source to share


1 answer


{{pathFor}}

uses the current data context to replace URL parameters with actual values, so you need to wrap the call inside a helper block {{#with}}

.

<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 object that returns an object with _id

, and this property will be used to populate the computed path.

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

      

+4


source







All Articles