IT박스

레일은 블록으로 부분적으로 렌더링

itboxs 2020. 7. 25. 10:42
반응형

레일은 블록으로 부분적으로 렌더링


패널 스타일을 제공하는 내가 작성한 html 구성 요소를 재사용하려고합니다. 다음과 같은 것 :

  <div class="v-panel">
    <div class="v-panel-tr"></div>
    <h3>Some Title</h3>
    <div class="v-panel-c">
      .. content goes here
    </div>
    <div class="v-panel-b"><div class="v-panel-br"></div><div class="v-panel-bl"></div></div>
  </div>

따라서 렌더링에는 블록이 필요합니다. 나는 다음과 같은 것을 할 수 있다고 생각했다.

# /shared/_panel.html.erb
<div class="v-panel">
  <div class="v-panel-tr"></div>
  <h3><%= title %></h3>
  <div class="v-panel-c">
    <%= yield %>
  </div>
  <div class="v-panel-b"><div class="v-panel-br"></div><div class="v-panel-bl"></div></div>
</div>

그리고 나는 다음과 같은 것을하고 싶다 :

#some html view
<%= render :partial => '/shared/panel', :locals =>{:title => "Some Title"} do %>
  <p>Here is some content to be rendered inside the panel</p>
<% end %>

불행히도 이것은이 오류와 함께 작동하지 않습니다 :

ActionView::TemplateError (/Users/bradrobertson/Repos/VeloUltralite/source/trunk/app/views/sessions/new.html.erb:1: , unexpected tRPAREN

old_output_buffer = output_buffer;;@output_buffer = '';  __in_erb_template=true ; @output_buffer.concat(( render :partial => '/shared/panel', :locals => {:title => "Welcome"} do ).to_s)
on line #1 of app/views/sessions/new.html.erb:
1: <%= render :partial => '/shared/panel', :locals => {:title => "Welcome"} do -%>
...

따라서 =분명히 블록을 좋아하지는 않지만 블록을 제거하면 아무것도 출력하지 않습니다.

아무도 내가 여기서 달성하려고하는 일을하는 방법을 알고 있습니까? 내 사이트의 여러 위치에서이 패널 html을 재사용하고 싶습니다.


위의 두 답변이 모두 효과가 있지만 (토니가 어떤 식 으로든 연결되는 예) 나는 위 게시물에서 가장 간결한 답변을 찾았습니다 (Kornelis Sietsma의 의견)

I 추측은 render :layout하지 정확히 내가 무엇을 찾고 있었다 :

# Some View
<%= render :layout => '/shared/panel', :locals => {:title => 'some title'} do %>
  <p>Here is some content</p>
<% end %>

와 결합 :

# /shared/_panel.html.erb
<div class="v-panel">
  <div class="v-panel-tr"></div>
  <h3><%= title -%></h3>
  <div class="v-panel-c">
    <%= yield %>
  </div>
</div>

Here's an alternative based on previous answers.

Create your partial on shared/_modal.html.erb:

<div class="ui modal form">
  <i class="close icon"></i>
  <div class="header">
    <%= heading %>
  </div>
  <div class="content">
    <%= capture(&block) %>
  </div>
  <div class="actions">
    <div class="ui negative button">Cancel</div>
    <div class="ui positive button">Ok</div>
  </div>
</div>

Define your method on application_helper.rb:

def modal_for(heading, &block)
  render(
    partial: 'shared/modal',
    locals: { heading: heading, block: block }
  )
end

Call it from any view:

<%= modal_for('My Title') do |t| %>
  <p>Here is some content to be rendered inside the partial</p>
<% end %>

You can use the capture helper, and even inline in the render call :

<%= render 'my_partial',
           :locals => { :title => "Some Title" },
           :captured => capture { %>
    <p>Here is some content to be rendered inside the partial</p>
<% } %>

and in shared/panel:

<h3><%= title %></h3>
<div class="my-outer-wrapper">
  <%= captured %>
</div>

which will produce:

<h3>Some Title</h3>
<div class="my-outer-wrapper">
  <p>Here is some content to be rendered inside the partial</p>
</div>

See http://api.rubyonrails.org/classes/ActionView/Helpers/CaptureHelper.html


Based on the accepted answer this is what worked well for me using Rails 4.

We can render a panel as such:

= render_panel('Non Compliance Reports', type: 'primary') do
  %p your content goes here!

enter image description here

This requires a helper method and a shared view:

helper method (ui_helper.rb)

def render_panel(heading, options = {}, &block)
  options.reverse_merge!(type: 'default')
  options[:panel_classes] = ["panel-#{options[:type]}"]

  render layout: '/ui/panel', locals: { heading: heading, options: options } do
    capture(&block)
  end
end

View (/ui/panel.html.haml)

.panel{ class: options[:panel_classes] }
  .panel-heading= heading
  .panel-body
    = yield

I think it will work (just did quick dirty test) if you assign it to a variable first and then output it.

<% foo = render :partial => '/shared/panel', :locals =>{:title => "Some Title"} do %>
<p>Here is some content to be rendered inside the panel</p>
<% end %>
<%= foo %>

참고URL : https://stackoverflow.com/questions/2951105/rails-render-partial-with-block

반응형