Rails 3中的多层嵌套布局 [英] Multiple Level Nested Layout in Rails 3
问题描述
我有一个带有全局应用程序布局文件application.html.haml
的应用程序.然后,我有多个控制器堆栈":用于我们的主站点,我们的管理门户和我们的业务站点.对于每个控制器,控制器都在模块内,并且都从相同的BaseController
继承.每个堆栈都有其自己的布局文件.在堆栈中,某些控制器也具有布局文件.
I have an application with a global application layout file application.html.haml
. I then have multiple "controller stacks": for our main site, our admin portal, and our business site. For each of these, controllers are within a module and all inherit from the same BaseController
. Each stack has it's own layout file. Within the stack, some controllers have layout files as well.
我希望所有视图(除非另行指定)都可以在多层嵌套布局内渲染:应用程序,堆栈",控制器".
I would like all views (unless otherwise specified) to render inside multiple levels of nested layouts : application, "stack", "controller".
例如,对于Site::BlogController#show
动作,我希望使用Rails进行渲染:
For example, for the Site::BlogController#show
action, I'd like rails to render:
/site/blog/show.html.haml
内/layouts/site/blog.html.haml
内/layouts/site.html.haml
内/layouts/application.html.haml
我很难理解如何将/layouts/site.html.haml
插入堆栈.看来,rails会自动将动作呈现在应用程序布局内的控制器布局内,但是,我看不到如何将布局插入"到渲染堆栈中.
I am having difficulty understanding how to insert /layouts/site.html.haml
into the stack. It appears as though automatically, rails will render the action inside the controller layout inside the application layout, however, I can't see how to "insert" layouts into the render stack.
非常感谢您的帮助,但是,我已经阅读了所有Rails指南,但均无济于事,因此,链接到
Any help is greatly appreciated, however, I have read all the rails guides to no avail, so a link to http://guides.rubyonrails.org/layouts_and_rendering.html#using-nested-layouts will not really be helpful.
推荐答案
我重新阅读了我发布的链接( http://guides.rubyonrails.org/layouts_and_rendering.html#using-nested-layouts ),并意识到我错过了一个关键细节.
I reread the link i posted ( http://guides.rubyonrails.org/layouts_and_rendering.html#using-nested-layouts ) and realized I missed a key detail.
<%= render :file => 'layouts/application' %>
所以,在Site::BaseController
中,我有一个呼叫给layout 'site'
,在/layouts/site.html.haml
中,我有一个呼叫
so, in Site::BaseController
I have a call to layout 'site'
and in /layouts/site.html.haml
I have
= content_for :footer do
-#Content for footer
= render :file => 'layouts/application'
然后在扩展了Site::BaseController
的Site::BlogController
中,我有layout 'site/blog'
,在/layouts/site/blog.html.haml
中我有
Then in Site::BlogController
which extends Site::BaseController
I have layout 'site/blog'
and in /layouts/site/blog.html.haml
I have
=content_for :header do
%h1 HELLO WORLD!
= render :file => 'layouts/site'
然后将呈现如问题中所述嵌套的布局.很抱歉在我的问题中遗漏了这个.我应该读得更近些.
This then renders the layouts nested as described in the question. Sorry for missing this in my question. I should've read closer.
这篇关于Rails 3中的多层嵌套布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!