7

In EmberJS, the main template file is the application.hbs. Any template rendered from the routes goes the the {{outlet}} of this main template file.

Now, I have another main template file, say print.hbs wherein the template design is very different from the application.hbs. How do I do this?

In the router file, I have:

App.Router.map(function() {

    this.resource('print', function() {
        this.route('display1');
        this.route('display2');
    });

    this.route('dashboard', {path: '/'});
    this.route('anything');
});

The routes dashboard and anything uses the application.hbs. What should I do to use print.hbs on the print route? Please help.

1 Answer 1

4

You can't easily change application template. Ember doesn't listen on templateName property changes and responds poorly when you try to re-render the template yourself.

A nice way to do this would be to use different partials within your application template, based on whether you are in the 'screen' or 'print' mode.

  <script type="text/x-handlebars">
    {{#if isPrint}}
      {{partial "application-print"}}
    {{else}}
      {{partial "application-normal"}}
    {{/if}}
  </script>

  <script type="text/x-handlebars" data-template-name="application-normal">
    <div id="app-normal">
      <h2>Normal template</h2>

      {{outlet}}
    </div>
  </script>

  <script type="text/x-handlebars" data-template-name="application-print">
    <div id="app-print">
      <h2>Print template</h2>

      {{outlet}}
    </div>
  </script>
App.ApplicationController = Ember.Controller.extend({
  isPrint: false,
  currentPathChange: function () {
    var currentPath = this.get("currentPath");
    var isPrint = currentPath ? currentPath.indexOf("print") === 0 : false;
    this.set("isPrint", isPrint);
  }.observes('currentPath').on("init")
});

This JSBin will demonstrate why this, unfortunately, doesn't work either. According to this bug report, Ember's handlebars gets confused when there are multiple outlet directives in the same page, even if they are in different #if scopes.

Until this gets fixed, I propose the following, slightly modified, solution.

Application template is empty. One template each for normal and print section.

  <script type="text/x-handlebars">
    {{outlet}}
  </script>

  <script type="text/x-handlebars" data-template-name="normal">
    <div id="app-normal">
      <h2>Normal template</h2>

      {{outlet}}
    </div>
  </script>

  <script type="text/x-handlebars" data-template-name="print">
    <div id="app-print">
      <h2>Print template</h2>

      {{outlet}}
    </div>
  </script>

In router, everything goes into normal and print resources. Normal resources are placed at /, so that all the links remain the same. No need for special coding in ApplicationController.

App.Router.map(function() {
  this.resource("print", function () {
    this.route("a");
    this.route("b");
  });

  this.resource("normal", {path: "/"}, function () {
    this.route("a");
    this.route("b");
  });
});

Working jsbin here.

3
  • Thanks for the reply @panta82... This means I have to change my routes to print.route1 and normal.route2 right?
    – Melvin
    Jun 12, 2014 at 13:44
  • You already have print resource. You just need to change the other routes to be inside screen or normal resource (or whatever you want to call it). In your example above, that would be dashboard and anything routes.
    – panta82
    Jun 12, 2014 at 21:06
  • According to information found on GitHub this should be already fixed in Ember Beta thanks to HTMLBars. However I haven't tested it(unable to get it work with Ember CLI).
    – Daniel
    Nov 30, 2014 at 15:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.