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:router
1.0.1
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With