1

I am using Meteor 1.2.1 + iron-router with autopublish turned on

I would like to construct an anchor href based on collection values returned by a helper and query params

Is it possible to do this in the template tag, e.g. query_param1 should be read from the URL?

<template name="dataEntry">
  {{#each data}}
    <li>
      <a href="/listing?name={{name}}&color=<query_param1>">
       Data name
      </a>
    </li>
  {{/each}}
</template>

Above, {{name}} is returned by the collection and query parameters are appended to that to create a full hyperlink for the href.

MasterAM
  • 16,283
  • 6
  • 45
  • 66
sami
  • 723
  • 2
  • 9
  • 23
  • Possible duplicate of [How to use URL parameters using Meteorjs](http://stackoverflow.com/questions/22120489/how-to-use-url-parameters-using-meteorjs) – Christian Fritz Feb 02 '16 at 23:35
  • not really - let me rephrase the question – sami Feb 02 '16 at 23:42
  • I think there is no url parser on the client, if that's what you meant. You'll need to parse the url search query yourself, but there are tons of javascript snippets out there for that. Once you have the value, you just return it from a helper and use the helper in your template. – Christian Fritz Feb 03 '16 at 00:29

2 Answers2

1

You can access an IronRouter param through a helper, e.g.:

Router.current().params.query_param1
Stephen Woods
  • 4,049
  • 1
  • 16
  • 27
  • 1
    yes, I am aware of that - the hyperlink needs to be made up of attribute values from a route and query params in the current URL, both – sami Feb 03 '16 at 00:23
1

You can use @Stephen's suggestion like this.

In your template html,

     <template name="dataEntry">
        {{#each data}}
            <li>
                <a href="/listing?{{queryParams}}">
                    Data name
                </a>
            </li>
        {{/each}}
    </template>

In your template JS,

Template.dataEntry.helpers({
   "queryParams": function () {
       var name = "";
       //get name from collection here like...
       //name = Meteor.user().profile.firstName;
       var color = Router.current().params.color;
       return "name=" + name + "&color=" + color;
    }
});

Or you can use two separate helpers

In your template html,

     <template name="dataEntry">
        {{#each data}}
            <li>
                <a href="/listing?name={{name}}&color={{color}}">
                    Data name
                </a>
            </li>
        {{/each}}
    </template>

In your template JS,

Template.dataEntry.helpers({
   "name": function () {
       var name = "";
       //get name from collection here like...
       //name = Meteor.user().profile.firstName;
       return name;
    },
   "color": function () {
       return Router.current().params.color;
    }
});
Kishor
  • 2,659
  • 4
  • 16
  • 34
  • 1
    thank you - that was it - 2 separate helpers, one for the collection, another to return query params - and I can concatenate them in the template – sami Feb 03 '16 at 13:26