Search code examples
javascriptember.jshandlebars.js

Get current route name on handlebar template in Ember.js 2


I needed to know the current route name so I can show or hide a global component that is created on my application.hbs.

This component should be hidden in just one route template, so this looks like the easier solution to me. Getting the current route name and if its equal "posts" it will not show the header-bar component.

Something like this:

{{#if currentRoute != 'login'}}
    {{header-bar}}
{{/if}}

{{outlet}}

Any idea on could I achieve this or something similar that solves my problem?


Solution

  • applicatiion controller has a property names currentRouteName. You can use it.

    If you want to use in another template you need to define that in controller.

    test.js (controller)

    init(){
      this._super(...arguments);
      let appCtrl = Ember.getOwner(this).lookup('controller:application');
      this.set('appCtrl', appCtrl);
    }
    

    test.hbs

    {{appCtrl.currentRouteName}}