14

In a Template Helper I get the current path from Iron.Router (iron:router) as follows:

Router.current().route.path()

This works fine, unless the route path does contain parameters (e.g. /client/:_id/edit). In that case the path() function returns null.

How do I get the current path within a Template Helper, when the route contains parameters?

I'm using Meteor 1.0 with iron:router1.0.1

Guns
  • 2,598
  • 2
  • 19
  • 49
Rico Leuthold
  • 1,865
  • 4
  • 32
  • 47

2 Answers2

14

I suppose the _id in your route comes from a collection, you need to pass route.path the document the route is based on.

Router.route("/client/:_id/edit",{
  name:"edit",
  data:function(){
    return MyCollection.findOne(this.params._id);
  }
});

<template name="edit">
  {{myHelper}}
  {{pathFor route="edit"}}
</template>

Template.edit.helpers({
  myHelper:function(){
    return Router.current().route.path(this);
  }
});

I suggest you use the default pathFor helper for rendering an URL in the app.

https://github.com/EventedMind/iron-router/blob/devel/Guide.md#pathfor

This helper is using the current data context (in this case MyCollection.findOne(this.params._id)) to extract route parameters.

But you can also use the path method from the route, which takes the document you want to generate the path for as first argument.

saimeunt
  • 22,472
  • 2
  • 53
  • 61
  • Perfect and detailed answer, thanks. I need the path in the context of navigation / highlighting certain items. I already had the setup exactly like you show it in the example, but did not know about the document (`this`) argument for the `path` function. – Rico Leuthold Nov 11 '14 at 08:03
8

Try the following alternative:

Iron.Location.get().path;

Works fine for me, when I need a path with parameters. Does not return the current route, though.

See Meteor Iron Router does not get Current Path from Route

Community
  • 1
  • 1
Samuel
  • 2,120
  • 1
  • 19
  • 31