为什么Rails使布局选择成为控制器关注而不是视图关注?我可以从视图中选择布局吗?

[英]Why does Rails make layout selection a controller concern instead of a view concern? Can I choose a layout from a view?


Maybe there's a technical/implementation reason for it, but it seems to me that "layout" is about as clearly part of the view layer as it can be, yet Rails seems to only allow specifying the layout at a controller level.

也许有技术/实现的原因,但在我看来,“布局”显然是视图层的一部分,但Rails似乎只允许在控制器级别指定布局。

My controller shouldn't care about layouts... the templates should. Is there any way to specify which layout to use from within the .erb file?

我的控制器不应该关心布局......模板应该。有没有办法在.erb文件中指定要使用的布局?

Something like:

就像是:

<%= with_layout :news_feed do %>

  <p>
    My markup in here.
  </p>

<% end %>

Or any other implementation you can think of... but the documentation only seems to refer to the layout from the controller perspective.

或者你能想到的任何其他实现......但是文档似乎只是从控制器的角度来看布局。

Maybe it wouldn't be too hard to implement a with_layout helper.

也许实现with_layout帮助器并不太难。

EDIT | I found exactly what I was looking for:

编辑|我找到了我正在寻找的东西:

<% render :layout => "some_layout" do %>
  <p>
    My markup here
  </p>
<% end %>

Now provided your ActionController has:

现在提供的ActionController具有:

class ApplicationController < ActionController::Base
  layout nil

Then each template can select its own layout, using this approach.

然后,每个模板都可以使用此方法选择自己的布局。

If you place a layout in app/layouts, with a name that matches the controller, that layout will be used, so you don't strictly have to specify if they are all the same. Either way, the dude how writes your templates now has full control of it, not the dude who's writing the controllers :)

如果在app / layouts中放置一个名称与控制器匹配的布局,那么将使用该布局,因此您不必严格指定它们是否完全相同。无论哪种方式,花花公子如何编写你的模板现在可以完全控制它,而不是那个正在编写控制器的家伙:)

2 个解决方案

#1


3  

I did some research and I did not find any solution similar to what you're looking for.

我做了一些研究,但没有找到任何与您正在寻找的解决方案类似的解决方案。

I think that might have to do with the fact that specifying a layout other than the default can be done by naming a file your_controllers_name.html.erb under app/views/layouts. This can remove any logic from your controller if the same layout is always used for a specific controller. Maybe this is what you're looking for? If you do not have a matching controller name for a layout, then the default application.html.erb will be used.

我认为这可能与指定非默认布局的事实有关,可以通过在app / views / layouts下命名文件your_controllers_name.html.erb来完成。如果始终为特定控制器使用相同的布局,则可以从控制器中删除任何逻辑。也许这就是你要找的?如果您没有匹配的布局控制器名称,则将使用默认的application.html.erb。

If you need to render a different layout conditionally, then that decision does need to be made in your controller. Otherwise you would push down the decision-making logic into your view. Your view should not have to make the decision about what layout it needs because the controller is there to make decisions and set up any necessary data that a view requires.

如果需要有条件地呈现不同的布局,则需要在控制器中做出该决定。否则,你会将决策逻辑推到你的视野中。您的视图不应该决定它需要什么样的布局,因为控制器可以做出决策并设置视图所需的任何必要数据。

#2


1  

Some statements:

一些陈述:

Layout is about rendering.
Layout can be called only once.
Views - are the top (last) level of rendering.
Controller - is a first level rendering layer.

布局是关于渲染。布局只能调用一次。视图 - 是最高(最后)渲染级别。控制器 - 是第一级渲染层。

So I can say that only place where we can set layout - is a controller. If we could set layout from views, so we will get problems with nested layouting, because each view (partial as well) will call its own layout.

所以我可以说只有我们可以设置布局的地方 - 是一个控制器。如果我们可以从视图设置布局,那么我们将遇到嵌套布局的问题,因为每个视图(部分也是如此)将调用它自己的布局。

It is easy and clear how controller sets layout. And it is unclear and messy if we are setting parent level view from its children.

控制器设置布局很简单明了。如果我们从孩子们那里设置父级别的观点,那就不清楚了。

What are you talking about - is yielding (content_for) and partials (render :partial). It is great tools and they work perfect.

你在说什么 - 是屈服(content_for)和部分(渲染:部分)。它是很好的工具,它们非常完美。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.itdaan.com/blog/2011/05/08/f287de0f23a8f67193fcfc20713f784d.html



 
© 2014-2018 ITdaan.com 粤ICP备14056181号