我无法找到执行以下操作的方法:
假设在我的 application.html.erb 中有以下内容
<div id="one" >
<%= yield %>
</div>
然后我想要另一个布局文件 asdf.html.erb
<div id="two">
<%= yield %>
</div>
我希望最终输出是
<div id="one>
<div id="two">
<%= yield %>
</div>
</div>
是否可以?谢谢。
最佳答案
我找到的最干净的解决方案 到目前为止来自这个仓库:https://github.com/rwz/nestive
我不想要整个 gem 。如果你像我一样,这就是我如何实现我想要的:
# application_helper.rb
# From https://github.com/rwz/nestive/blob/master/lib/nestive/layout_helper.rb
def extends(layout, &block)
# Make sure it's a string
layout = layout.to_s
# If there's no directory component, presume a plain layout name
layout = "layouts/#{layout}" unless layout.include?('/')
# Capture the content to be placed inside the extended layout
@view_flow.get(:layout).replace capture(&block)
render file: layout
end
那你留着
/layouts/application.html.erb
不变!您可以创建其他布局。就我而言
/layouts/public.html.erb
和 /layouts/devise.html.erb
:# public.html.erb
<%= extends :application do %>
<%= render 'partials/navbar' %>
<div class="container margin-top">
<%= yield %>
</div>
<% end %>
# devise.html.erb
<%= extends :public do %>
<div class="col-sm-6 col-sm-offset-3">
<%= yield %>
</div>
<% end %>
奇迹般有效!我仍然微笑我终于找到了一个干净的解决方案。
关于ruby-on-rails - Rails 中的嵌套布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20480961/