Conditional rendering of header based upon action



Every page in my ember app renders a header using this application.hbs:

{{partial "header"}}


{{partial "footer"}}

However, for a couple of actions like e.g sign-in and signup I want a different much simpler header than all the other actions. This is similar to what does when you click Log In. What is the recommended practices for implementing this?

Custom computed property for route specific values

Why don’t you use a simple conditional statement?

{{#if isUserLoggedIn}}
    {{partial "header_v1"}}
    {{partial "header_v2"}}

I’m not sure that it’s a good solution, but at least…


In this solution, how do I make the partial conditional upon which controller or action is currently rendered? The header is in my case not conditional upon if the user is authenticated or not.


You could try to use the render method available to routes. The render method allows you to control which templates, controller and model is used for specific parts of your application. This combined with named outlets might be what you are after.

Route render api:

Named outlets take the form:

{{outlet header}}

So when you enter or exit routes you could render the specific template you need for the header.


currentPath is available on the ApplicationController so you can do something like:

App.ApplicationController = Ember.Controller.extend({
    headerPartial: function(){
        return this.get("currentPath") === '/login' ? 'simpleHeader' : 'fullHeader';


employing something similar to @opsb’s route lookup, I was able to get it to work with the following stubs to show the login template without the wrapping content (headers, navbars, etc):


import Ember from 'ember';

export default Ember.Controller.extend({
    isLoginRoute: Ember.computed.equal('currentRouteName', 'login')


<div {{bind-attr class="isLoginRoute::wrapper"}}>
{{#unless isLoginRoute}}
    {{partial "header"}}
{{#unless isLoginRoute}}
    {{partial "footer"}}

the class binding was used to ensure no wrapper affects the login template but would otherwise be active but probably there’s a better way out there to achieve this.


@asabjorn a thread I made earlier has a solution to this problem.

probably the easiest thing for you is to have a computed property on your application controller:

showReducedHeader: function(){
        //array of all routes without header
        var reducedHeader = [
        //read as reducedHeader.contains('currentPath')
        return ($.inArray(this.get('currentPath'), reducedHeader) != -1);

and then your template

{{#if reducedHeader}}
    {{partial 'reducedHeader'}}
    {{partial 'header'}}

or using my routeVal computed property defined in the link:

routeVals: [
            route: 'login',
            reducedHeader: true
            route: 'logout',
            reducedHeader: true
reducedHeader: routeVal('routeVals', 'reducedHeader')


I’m not sure why back then I was using the {{unless}} … {{/unless}} blocks but today this is what I have in my login template instead:

{{#if isLoginRoute}}
  {{partial "header"}}
	{{#each flashMessages.queue as |flash|}}
	  {{flash-message flash=flash messageStyle="bootstrap"}}
  {{partial "footer"}}