Search code examples
meteoriron-routerspacebars

Distinct layouts for distinct user types


I have two distinct userTypes 'reader' and 'publisher'. This information is stored in Meteor.users document Meteor.users.userType: 'publisher' or it can Meteor.users.userType:'reader'

How can I dynamically add elements to the appBody depending on userType?

The data context and things like menu items will of course be different for userTypes

I have referenced https://github.com/EventedMind/iron-dynamic-template

HTML:

<head>
 <title>Site</title>
</head>

<template name="appBody"> //this is layoutTemplate
    <div class="navbar navbar-fixed-top navbar-custom">
        <div class = "container">
            <ul class="nav navbar-nav navbar-right">
                <li><a href="#">menu title A</a></li>
                <li><a href="#">menu title B</a></li>
                ***DYNAMICALLY ADD LIST ELEMENT DEPENDING ON WHICH 'userType' IS LOGGED IN HERE***
            </ul>
        </div>
    </div>
    {{> UI.dynamic template=currentUser.userType }}
</template>


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


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

router.js

Router.configure({
    layoutTemplate: 'appBody',
    loadingTemplate: 'appLoading'
});

Solution

  • I have in a way broke the app into a logged in state and not logged in state. When the user does log in, depending on their userType, I either render a 'reader' template or a 'publisher' template. Both of these now behave as the layout templates for each user so to speak. Then I can yield in templates into these using iron:router.

    Router.configure({
    layoutTemplate: 'parent',
    loadingTemplate: 'appLoading'
    });
    
    
    <template name="parent">
      {{#if currentUser}}
      {{> UI.dynamic template=currentUser.userType }} //this returns 'reader' or 'publisher'
      {{else}}
      {{> notLoggedIn}}
      {{/if}}
    </template>
    
    <template name="notLoggedIn">
    //unique styling for non logged in users such as join and log in.
    {{> yield}}
    </template>
    
    <template name="reader">
    //unique styling and custom reader navigation menu
    {{> yield}}
    </template>
    
    <template name="publisher">
    //unique styling and custom publisher navigation menu
    {{> yield}}
    </template>
    

    Now I can render any template into the yield region from Router.route :))

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