Search code examples
meteoriron-router

Change some html in layout template when on a certain route (with iron-router and meteor.js)


I have a template I have set up as my layout using iron-router:

Router.configure({ layoutTemplate: 'main' });

Within this layout, I have a line of html that I would like to be changed on certain routes.

My ideas is doing something like:

{{#if landing }}
<div id="page-wrapper">
{{/if}}

However, how do I implement this for a certain route? I want this variable to be true on every route except for 1.


Solution

  • I think that this kind of "change the template based upon what route I'm on" logic fits best into the use of a template for that route (and any others that will make this same change. Depending on the change required, you may be able to call in your base Template into the modified template. Example:

    Router.route('/yourSpecialRoute', function(){ 
      this.layout('OtherLayout'); 
    });
    

    See the Layout docs - I borrowed the syntax of Setting Region Data Contexts

    Having said, if you prefer not to switch layouts per route, consider setting a data on your route (something like {data: item} as shown here in the iron:route readme which can then be read by a global helper (Template.registerHelper syntax) - this will at least make it consistent across your routes / templates.