Victor Ferreira
Victor Ferreira

Reputation: 6449

Meteor's Iron Router: get query parameter on both Client and Server Side

there

In my application, if someone pass a parameter on the URL I want to do different things on the template.

I know I can get on server side a query string using this.params.query but how can I pass it to client OR get this value on client-side?

In my case I will send an optional redirect on the URL, and if it was passed, after the main task, my app will redirect the user to the url given. But I just know how to see redirect on server side, not on client, so this information get lost when I, for example, submit a form

Could you help me?

Upvotes: 1

Views: 912

Answers (2)

k.chao.0424
k.chao.0424

Reputation: 1191

You can access the router params in your template with:

Router.current().params.query

Upvotes: 1

Quin
Quin

Reputation: 541

Maybe in your route you can do like this:

onBeforeAction: function() {
    Session.set("query", this.params.query);
    this.next();
},

Then somewhere on the client you can make a helper like this:

Template.yourTemplateName.helpers({
  query: function() {
    return Session.get("query");
  }
});

I'm not sure what to do with your redirect, but maybe it will work in the helper. So your html would be something like:

{{#if query}}{{query}}{{/if}}

And in the helper you could possibly replace "return Session.get("query")" with something like "Router.go('/wherever-you-want-to-go');". Or just write another helper to run in your html if the query is positive.

Upvotes: 0

Related Questions