Michael McC
Michael McC

Reputation: 689

Meteor and iron-router: dynamically specify a template?

Every example that I have seen with iron-router specifies the template name with a string. Is it possible to do this with a variable? Suppose you have several routes that all use the same dynamic path, and the same data function, but they all need different templates. Is there a way to do this without specifying a different route for every template (which would also mean changing the path I use)?

Upvotes: 0

Views: 717

Answers (2)

David Weldon
David Weldon

Reputation: 64342

You can programmatically specify things like the template and the layout with a custom action function. The example below demonstrates showing a particular template if the required document is found based on an id in the route. You can use the same semantics for both routes and controllers.

var postsController = RouteController.extend({
  waitOn: function() {
    return Meteor.subscribe('post', this.params._id);
  },
  action: function() {
    if (Posts.findOne(this.params._id)) {
      this.layout('postsLayout');
      this.render('posts');
    } else {
      this.render('notFound');
    }
  }
});

Upvotes: 1

imslavko
imslavko

Reputation: 6676

Once 0.8.2 is released it should be trivial to do with UI.dynamic even without Iron-Router: https://github.com/meteor/meteor/issues/2123.

Upvotes: 1

Related Questions