是否可以在 Rails 中使用部分渲染为包装器?
我想渲染这样的结构:
<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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这可能是
capture
方法的一个很好的用途。我只熟悉 ERB,但总体思路如下:
然后将变量传递到部分中:
在部分中,吐出
struct
变量:Reset
struct
当您渲染部分视图时(或者更合适,在助手中?),在视图中多次。This might be a good use of the
capture
method.I'm only familiar with ERB, but here is the general idea:
Then pass the variable into the partial:
And within the partial, spit out the
structure
variable:Reset
structure
multiple times within the view as you render partials (or maybe more appropriately, in a helper?).我使用了以下内容(Rails 4,但我认为它也应该适用于 Rails 3):
。
I've used the following (Rails 4, but I think it should work with Rails 3 too):
.