Rails render partial with block
Próbuję ponownie użyć komponentu html, który napisałem, który zapewnia stylizację paneli. Coś w stylu:
<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>
Widzę, że render zajmuje blok. Pomyślałem wtedy, że mogę zrobić coś takiego:
# /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>
A ja chcę zrobić coś takiego:
#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 %>
Niestety to nie działa z tym błędem:
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 -%>
...
Więc nie podoba się =
oczywiście z blokiem, ale jeśli go usunę, to po prostu nic nie wyświetli.
Robi ktoś wie, jak zrobić to, co próbuję osiągnąć? Chciałbym ponownie użyć tego html panelu w wielu miejscach na mojej stronie.
5 answers
Podczas gdy obie powyższe odpowiedzi działają (dobrze przykład, do którego tony i tak linkuje), znalazłem najbardziej zwięzłą odpowiedź w powyższym poście (komentarz Kornelisa Sietsmy)
Chyba render :layout
robi dokładnie to czego szukałem:
# Some View
<%= render :layout => '/shared/panel', :locals => {:title => 'some title'} do %>
<p>Here is some content</p>
<% end %>
Połączone z:
# /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>
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-03-04 12:14:47
Oto alternatywa oparta na wcześniejszych odpowiedziach.
Utwórz swoją część na 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>
Zdefiniuj swoją metodę na application_helper.rb
:
def modal_for(heading, &block)
render(
partial: 'shared/modal',
locals: { heading: heading, block: block }
)
end
Wywołaj to z dowolnego widoku:
<%= modal_for('My Title') do |t| %>
<p>Here is some content to be rendered inside the partial</p>
<% end %>
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-12-08 21:12:42
Możesz użyć helpera przechwytywania, a nawet inline w wywołaniu renderowania:
<%= render 'my_partial',
:locals => { :title => "Some Title" },
:captured => capture { %>
<p>Here is some content to be rendered inside the partial</p>
<% } %>
I w shared / panel:
<h3><%= title %></h3>
<div class="my-outer-wrapper">
<%= captured %>
</div>
Który da:
<h3>Some Title</h3>
<div class="my-outer-wrapper">
<p>Here is some content to be rendered inside the partial</p>
</div>
Zobacz http://api.rubyonrails.org/classes/ActionView/Helpers/CaptureHelper.html
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-03-19 06:24:47
Bazując na zaakceptowanej odpowiedzi, to działa dobrze dla mnie używając Rails 4.
Możemy renderować panel jako taki:
= render_panel('Non Compliance Reports', type: 'primary') do
%p your content goes here!
Wymaga to metody pomocniczej i widoku współdzielonego:
Metoda Helper (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
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-05-22 11:18:32
Myślę, że będzie działać (po prostu zrobił szybki brudny test), jeśli przypisać go do zmiennej najpierw, a następnie wyjście go.
<% foo = render :partial => '/shared/panel', :locals =>{:title => "Some Title"} do %>
<p>Here is some content to be rendered inside the panel</p>
<% end %>
<%= foo %>
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-06-01 15:51:07