是否可以在 Rails 中使用部分渲染为包装器?

发布于 2024-10-12 18:49:25 字数 751 浏览 5 评论 0原文

我想渲染这样的结构:

<tag1>
  <tag2 someattribute="somevalue">
    <.. lot of things inside ..>
  </tag2>
</tag1>

<tag1>
  <tag2 someattribute="someothervalue">
    <.. different inside things inside ..>
  </tag2>
</tag1>

tag1、tag2 是相同的,它们只是参数化了。代码的内部部分发生了变化。我尝试像这样实现上面的东西(haml):

%div{id:['products', id]}
  .products_content
    %div{id:['products', id, 'content'], class:'products_mask'}
      = yield

这是部分_content_head.html.haml,它是从模板调用的:

= render 'shared/content_head', id: 'all' do
  %h3= Title
  %p= Body of the text.

我的理论是部分内部的yield会导致传递的块的渲染没有得到证明。有没有办法使用部分作为代码包装器?您能给我建议一些解决方案吗?谢谢。

I would like to render structures like this:

<tag1>
  <tag2 someattribute="somevalue">
    <.. lot of things inside ..>
  </tag2>
</tag1>

<tag1>
  <tag2 someattribute="someothervalue">
    <.. different inside things inside ..>
  </tag2>
</tag1>

The tag1, tag2 are the same, they are just parametrized. The inner part of the code changes. I tried to implement the thing above like that (haml):

%div{id:['products', id]}
  .products_content
    %div{id:['products', id, 'content'], class:'products_mask'}
      = yield

This was the partial _content_head.html.haml, which is called from a template:

= render 'shared/content_head', id: 'all' do
  %h3= Title
  %p= Body of the text.

My theory that yield inside the partial would lead to rendering of the passed block did not prove. Is there a way to use partials as code wrappers? Can you suggest me some solution how to reach this? Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

倒数 2024-10-19 18:49:25

这可能是 capture 方法的一个很好的用途。

我只熟悉 ERB,但总体思路如下:

<% structure = capture do %>
  <h3>Title</h3>
  <p>Body of text</p>
<% end %>

然后将变量传递到部分中:

<%= render 'shared/content_head', :structure => structure %>

在部分中,吐出 struct 变量:

<%= structure %>

Reset struct当您渲染部分视图时(或者更合适,在助手中?),在视图中多次。

This might be a good use of the capture method.

I'm only familiar with ERB, but here is the general idea:

<% structure = capture do %>
  <h3>Title</h3>
  <p>Body of text</p>
<% end %>

Then pass the variable into the partial:

<%= render 'shared/content_head', :structure => structure %>

And within the partial, spit out the structure variable:

<%= structure %>

Reset structure multiple times within the view as you render partials (or maybe more appropriately, in a helper?).

ま昔日黯然 2024-10-19 18:49:25

我使用了以下内容(Rails 4,但我认为它也应该适用于 Rails 3):

<%# app/views/users/_edit.html.erb %>
<%= render layout: 'modal_wrapping' do |f| %>
  <%= f.input :email %>
  ...
<% end %>

<%# app/views/users/_modal_wrapping.html.erb %>
<div id='modal'>
  <%= simple_form_for @user do |f| %>
    <%= yield f %>
  <% end %>
</div>

I've used the following (Rails 4, but I think it should work with Rails 3 too):

<%# app/views/users/_edit.html.erb %>
<%= render layout: 'modal_wrapping' do |f| %>
  <%= f.input :email %>
  ...
<% end %>

.

<%# app/views/users/_modal_wrapping.html.erb %>
<div id='modal'>
  <%= simple_form_for @user do |f| %>
    <%= yield f %>
  <% end %>
</div>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文