Search code examples
meteoriron-router

Meteor pass variable, iron route


Im getting started Meteor , I use iron router to manipulate route.. so I want to pass a variable to template:

Router.route('/foo', function(){
  this.render('foo', {name: 'Stack'});
});

how i can show the variable name in the template foo:

<template name="foo">
    <h2>Hi bro, how i can show the variable name here ?? </h2>
</template>

my project folder as the following structure:

/client
---/views
------foo.html
---/layout
------layout.html
/public
/server

layout.html:

<template name="layout">
  {{> yield}}
</template>

any solutions please :)


Solution

  • In your routing:

    Router.route('/foo', function(){
      this.render('foo', {data: {name: 'Stack'}});
    });
    

    In your template

    <template name="foo">
        <h2>Hi bro, how i can show the variable name here ?? </h2>
        <p>Like this --> {{name}}</p>
    </template>
    

    You can pull variables from the route too:

    Router.route('/foo/:someName', function(){
      this.render('foo', {data: {name: this.params.someName}});
    });
    

    See Iron Router docs for more info