简体   繁体   中英

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?

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.

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.

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
    });

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()
 
  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM