2010-01-25 15 views
5

내 웹 페이지는 두 부분으로 구성되어 있습니다. 맨 위와 맨 아래를 가정 해 봅시다 (머리글과 바닥 글은 페이지 전체에서 일관됩니다). 동작에 따라 해당 부분을 동적으로 생성하는 가장 좋은 방법은 무엇입니까?두 파트 레일즈 레이아웃

내가 생각해 낸 한 가지 방법은 맨 위 부분을 볼 수 있고 바닥 부분을 보는 것입니다. 상단에 대한 레이아웃 호출 산출물에서 하단에 대한 렌더링 부분. 부분의 이름은 동작에 따라 동적으로 대체됩니다.

가장 좋은 방법은 아닌지 잘 모릅니다.

답변

8

나는 당신의 아이디어가 훌륭하다고 생각합니다. 귀하의 견해에 당신은 할 수 :

<%- content_for :top do -%> 
    […] 
<%- end -%> 

<%- content_for :bottom do -%> 
    <%= render @partial_name %> 
<%- end -%> 

물론 부분이 존재하는지 확인하고 일부 기본 동작을 제공해야합니다. 하지만 어쨌든 당신이 알고 있다고 생각합니다.

그리고 레이아웃에

: 여기

<div id="top"> 
    <%= yield :top %> 
</div> 

<div id="bottom"> 
    <%= yield :bottom %> 
</div> 
+0

죄송합니다. 처음에는 질문을 제대로 읽지 않았습니다. 내 대답을 편집하여 더 많이 당신과 관련된 것입니다. –

+0

나는 당신의 접근 방식을 좋아한다. 감사. –

1

내가 과거에 사용했던 뷰 DSL의 매우 단순화 된 버전입니다. 우리를 위해 잘 일했습니다. 실제로 우리는 도우미 메서드를 매개 변수화하여 많은 레이아웃 부분을 즉석에서 선택할 수 있도록했습니다 (사이드 바, 여러 열 등이있는 페이지가 있음).

# app/views/shared/_screen.erb 
<div id="screen"> 
    <div class="screen_header"> 
<%= yield :screen_header %> 
    </div> 
    <div class="screen_body"> 
<%= yield :screen_body 
    </div> 
    <div class="bottom"> 
    <%= yield :footer %> 
    </div> 
</div> 

# app/helpers/screen_helper.rb 
module ScreenHelper 

def screen(&block) 
    yield block 
    concat(render :partial => 'shared/screen') 
end 

def screen_header 
    content_for :screen_header do 
    yield 
    end 
end 

def screen_body 
    content_for :screen_body do 
    yield 
    end 
end 

def footer 
    content_for :footer do 
    yield 
    end 
end 
end 

# app/views/layouts/application.erb 
# only showing the body tag 
<body> 
    <%= yield :layout 
<body> 

# Example of a page 
# any of the sections below (except screen) may be used or omitted as needed. 
# app/views/users/index.html.erb 
<% screen do %> 
    <% screen_header do %> 
    Add all html and/or partial renders for the header here. 
    <%end%> 
    <% screen_body do %> 
    Add all html and/or partial renders for the main content here. 
    <% end %> 
    <% footer do %> 
Add all the html and/or partial renders for the footer content here. 
    <% end %> 
<% end %> 
관련 문제