How to pass a parameter to pathFor in Handlebars for Iron-Router with Meteorite?
Asked Answered
S

3

29

I have a simple route with a parameter:

this.route('article', {
        path: '/article/:_id',
        data: function() { return Articles.findOne(this.params._id); }
    });

I would like to have use the pathFor handlebars helper here with the _id:

{{#each articles}}
<li><a href="{{pathFor 'article' _id}}">{{title}}</a></li>
{{/each}}

This doesnt work for passing the _id tag into the url though...

Suomi answered 25/8, 2013 at 1:31 Comment(0)
S
60
<li><a href="{{pathFor 'article' _id=this._id }}">{{title}}</a></li>

Thats how you pass a parameter

Suomi answered 25/8, 2013 at 1:47 Comment(5)
No, this creates /article/abcd/?_id=abcd.Quadrennial
Perhaps you have an old version.Quadrennial
NOTE: This has changed a little bit. You now need to use the 'query'-Parameter github.com/iron-meteor/iron-router/blob/devel/Guide.md#pathforChopstick
Additional note: If you want to use dynamic query-Parameter you can use the helper proposed in this issue in GitHub github.com/iron-meteor/iron-router/issues/589Chopstick
@Chopstick You sir deserve a cookie.Alienist
P
13

In your example you don't need to pass any parameters. The pathFor helper will automatically use the current data context and grab the property from that object. Since you have an {{#each articles}} iterator, the current data context for pathFor will be set to the current item in the list (an article). The article object should have an _id property on it, so the path helper will just work. If you wanted to change the data context, you could pass another object as a first parameter to the pathFor helper, or you could use the {{#with anotherDataContextObject}} block helper.

In v0.5.4, you could also use the Handlebars hash key values to replace the context object. In the dev branch this is no longer true. If you provide key value pairs as parameters in your Handlebars helper, those will be appended to the query string. To illustrate here's an example for the change in dev branch:

{{pathFor 'article' article q=some+query}}
//=> "/article/abcd/?q=some+query

Or

{{#with article}}
{{pathFor 'article' q=some+query}}
//=> "/article/abcd/?q=some+query
{{/with}}
Pule answered 25/9, 2013 at 6:33 Comment(1)
In {{pathFor 'article' article q=some+query}}, can the second article be called anything? Where can I find the documentation?Krefeld
S
0

In Meteor 1.3.X I'm using the options for the pathFor helper.

{{pathFor route='' data= query='' hash=''}}

Siusiubhan answered 27/9, 2016 at 16:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.