2013-05-30 18 views
0

我網站上的所有頁面都使用應用程序佈局。我希望我的靜態頁面也使用靜態頁面佈局。我如何一起使用兩種佈局?如何在Rails中爲頁面使用2個佈局?

應用程序佈局:

<html> 
<head></head> 
<body> 
<%= yield %> 

</body> 
</html> 

靜態頁面佈局:

<div class="static"> 
</div> 

靜態頁面:

<p>Hello</p> 

我想導致頁面:

<html> 
    <head></head> 
    <body> 

    <div class="static"> 
    <p>Hello</p> 
    </div> 

    </body> 
</html> 

回答

0

喜在控制器使用layout

class StaticPagesController < ..... 

# you can apply only,execpt if you want 
layout 'static' 

    def home 

    end 
end 
2

http://guides.rubyonrails.org/layouts_and_rendering.html#using-nested-layouts

裏面你的第二個佈局,靜態頁面佈局:

靜態佈局

<%= render :template => 'layouts/application' %> 

應用佈局

<%= yield %> 

控制器要使用靜態佈局

class AController 
    layout 'static_layout' 

static_layout應該在視圖/佈局。

我想你可以使用太多佈局約定:

class StaticController 

和文件app/views/layouts/static.html.erb

或者在render呼叫選擇佈局:

render 'something', layout: 'static' 
+0

這將是簡單的代碼,但它無法正常工作。 –

+0

你嘗試過什麼? – juanpastas

+0

我嘗試使用和不使用「yield」的靜態佈局,以及使用和不指定「yield static」,但我無法在應用程序佈局中正確嵌套它。 –

1

您需要了解所使用的yield

applicati on.html.erb

<html> 
    <head></head> 
    <body> 
    <% if @static %> 
     <div class="static"> 
     <%= yield :static %> 
     </div> 
    <% end %> 
    <%= yield %> 
    </body> 
</html> 

static_page.html.erb

<% content_for :static do %> 
    <p>Hello</p> 
<% end %> 

只有瘦,你需要在靜態頁面控制器動作,現在照顧集的@static

相關問題