简体   繁体   English

有什么方法可以在铁路由器中渲染没有布局的模板?

[英]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. 在这种情况下,“ loginPage”将呈现到siteLayout模板的yield区域中。 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. 告诉Iron Router使用(或不使用)哪种布局的地方是在layoutTemplate参数中,而不是在onBeforeAction函数中。 The trick is to make layoutTemplate into a self-executing anonymous function: 诀窍是使layoutTemplate成为一个自执行的匿名函数:

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. 在此示例中,如果用户未登录,则layoutTemplate设置为null并且Iron Router直接渲染到主体中。 Otherwise, siteLayout is used. 否则,将使用siteLayout 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. 重要的部分是括号,尤其是final () ,它使函数被求值并返回字符串或null ,而不是函数定义本身。

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. 请注意,您可以为未登录的用户创建替代布局,例如,它具有最小的导航栏,而不是完整菜单,然后在第四行上放置它而不是null

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 ; https://github.com/EventedMind/iron-router/issues/600https://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. 但是,可以在渲染之前使用this.setLayout控制布局。 In Iron Router 0.7.1, what you basically want is the following: 在Iron Router 0.7.1中,您基本上需要以下内容:

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. 请注意,这里实际上忽略了layoutTemplate设置,但是当我上面提到的合并请求合并时,您可以取出第二个setLayout

You can specify the default template layoutTemplate for each route. 您可以为每个路由指定默认模板layoutTemplate。

In this case we will just set the layoutTemplate to null for the login template only. 在这种情况下,我们仅将登录模板的layoutTemplate设置为null。 The other routes will be rendered with the default layoutTemplate except the login template. 除登录模板外,其他路由将使用默认的layoutTemplate呈现。

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

It's actually pretty simple: just pass null as your layoutTemplate parameter: 实际上非常简单:只需将 null作为您的 layoutTemplate参数传递:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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