0

I have a Meteor template in HTML-file:

<template name='main'> </template>

I rendered it using Iron router:

Router.route('/', function () { this.render('main'); });

Now I want to render another template to replace 'main' template. How to do it?

MRocklin
  • 55,641
  • 23
  • 163
  • 235

1 Answers1

1

Obviously you do not want another route?

If not you can use a reactive var in the router. When you change the variable it will run again and render your other template. See http://eventedmind.github.io/iron-router/#hooks

var OnBeforeActions;

OnBeforeActions = {
    whichMain: function() {
       if (reactiveVar) {
        this.render('otherMain');
      }
       else 
        this.next()  ;
    }
};

Router.onBeforeAction(OnBeforeActions.whichMain, {
    only: ['Main']
});

Alternatively use a dynamic template inside your main router.

https://www.discovermeteor.com/blog/blaze-dynamic-template-includes/

Rune Jeppesen
  • 1,121
  • 13
  • 22
  • Thank you! Could you show, how to use a reactive var in the router? I think it's that I need, but I don't understand how to implement it. I do not want another route in this case due to the app logic. – Konstantin Varik Oct 06 '14 at 21:07
  • you're welcome - please look at this: http://eventedmind.github.io/iron-router/#hooks Where Meteor.user is the reactive var that makes it rerun. I don't have time right now to create example - I hope to, have tomorrow. – Rune Jeppesen Oct 06 '14 at 21:25
  • thanks! If I understand correctly, I can make `if (reactiveVar) { this.render(reactiveVar); }` And every time I assign `reactiveVar='anotherMain'` the page will rerender with _anotherMain_ template, right? – Konstantin Varik Oct 07 '14 at 05:57
  • You would like to make the route name dynamic? (will you be using many differents routes?) I don't know if that works - guess it will! If that works - you actually don't need the 'if (reactiveVar)' part. See http://eventedmind.github.io/iron-router/#hooks – Rune Jeppesen Oct 07 '14 at 08:01
  • No I don't need many different routes. I need many different templates for the same route. And I need to change them on user click. – Konstantin Varik Oct 07 '14 at 13:42