user930836
user930836

Reputation: 164

Is there any way to render a template without layout in iron-router?

Just look my controller code(coffeescript):

class @BasicController extends RouterController
  layoutTemplate: "siteLayout"
  onBeforeAction: (pause)->
    unless Meteor.user()
      @render("loginPage")   #----------------->here
      pause()

In this case, the "loginPage" is rendered into yield region of siteLayout template. But I need it rendered without layout. how to implement that?

Upvotes: 2

Views: 699

Answers (4)

Kristiyan Lukanov
Kristiyan Lukanov

Reputation: 229

You can specify the default template layoutTemplate for each route.

In this case we will just set the layoutTemplate to null for the login template only. The other routes will be rendered with the default layoutTemplate except the login template.

Router.route('login',  {
        layoutTemplate: '' //set the layout template to null
    });

Upvotes: 1

Andrew Mao
Andrew Mao

Reputation: 36940

This is a pretty long discussion about this in https://github.com/EventedMind/iron-router/issues/600 and https://github.com/EventedMind/iron-router/issues/607; I've submitted a patch for this and you can expect the behavior to change.

However, you can use this.setLayout to control the layout before rendering. In Iron Router 0.7.1, what you basically want is the following:

class @BasicController extends RouteController
  layoutTemplate: "siteLayout"
  onBeforeAction: (pause) ->
    unless Meteor.user()
      @setLayout(null)
      @render("loginPage")
      pause()
    @setLayout("siteLayout")

Note that the layoutTemplate setting is actually ignored here but you can take out the second setLayout when the pull request I mentioned above is merged.

Upvotes: 1

Hubert OG
Hubert OG

Reputation: 19544

It's actually pretty simple: just pass null as your layoutTemplate parameter:

class @BasicController extends RouterController
  layoutTemplate: null
  onBeforeAction: (pause) ->
    unless Meteor.user()
      @render("loginPage")
      pause()

Upvotes: 0

Geoffrey Booth
Geoffrey Booth

Reputation: 7366

The place to tell Iron Router which layout to use (or not to use) is in the layoutTemplate parameter, not inside the onBeforeAction function. The trick is to make layoutTemplate into a self-executing anonymous function:

class @BasicController extends RouteController
  layoutTemplate: (->
      unless Meteor.user()
        return null
      else
        return "siteLayout"
    )()

  onBeforeAction: (pause) ->
    unless Meteor.user()
      @render("loginPage")
      pause()

In this example, if the user isn't logged in the layoutTemplate is set to null and Iron Router renders directly into the body. Otherwise, siteLayout is used. The important part is the parentheses, especially the final () that causes the function to be evaluated and return a string or null, not the function definition itself.

Note that you could create an alternative layout for non-logged-in users, say that has a minimal navbar instead of a full menu, and put that instead of the null on the fourth line.

Upvotes: 1

Related Questions