Search code examples
javascriptmeteoriron-routermeteor-helper

MeteorJs Template conditions to display different template


I have a meteorjs default layout file and i am routing to different pages using iron-route, What i am trying to do is not show navigation on the home page. I can do this by using two separate files but i want to do this with single file.

<template name="layout">

    <head>
        <title>
            {{title}}
        </title>
    </head>
    <body>

        {{> navigation }}

        <div class="nav_padding">
            {{> yield}}
        </div>


        {{> footer}}

    </body>

    </template>

My route

    Router.route('/', function(){
        this.layout('homeLayout');
        this.render('home', {
            data:{title: 'some'}
        });
    });

Solution

  • There are a few ways you can do this, but here's a simple solution...

    Add a helper to your layout template which indicates if the nav should be shown based on the route name:

    Template.myLayout.helpers({
      showNav: function() {
        return Router.current().route.getName() !== 'home';
      }
    });
    

    Modify your template to conditionally show the navigation template:

    <template name="myLayout">
      {{#if showNav}}
        {{> navigation }}
      {{/if}}
      <div class="nav_padding">
        {{> yield}}
      </div>
      {{> footer}}
    </template>
    

    In order for your route to be named you may need to modify your route to something like this:

    Router.route('/', {
      name: 'home',
      template: 'home',
      layoutTemplate: 'myLayout',
      data: {title: 'some'}
    });