Search code examples
meteoriron-router

Iron Router URL Extension


So I have a user profile and would like to have a "Favorites" section linked to it where favorited posts are displayed.

My user profile URL looks like this in Iron Router:

path: '/:username'

And my "Favorites" URL looks like this:

path: '/:username/favorites'

And I'd like my user profile to have this:

<template name="userProfile">
   <a href={{pathFor 'favorites'}}>Favorites</a>
</template>

Is there a quick solution to essentially "extend" the user profile route by adding "/favorites" on to it? Would I need to use "Router.current().url" in a template helper, or is there an easier way?


Solution

  • The pathFor helper takes the params for the URL from the context it is called from. So in your case it will search for username inside the userProfile-template instance. So there are two ways of going about this.

    • You could add username to the data context
    • Use a spacebars with-block

    Here's the code using a with-block

    {{ #with currentUser }}
      {{ pathFor 'favorites' }}
    {{ /with }}
    

    EDIT

    currentUser is a global helper. It returns the current users data.