Rails 视图:如何将变量传递给部分布局?
在我的一个视图中,我将布局应用于代码块:
# In app/views/sessions/new.html.erb
<% render :layout => 'home/shadow_box' do %>
#... code for sign in form here
<% end %>
布局是一个 div,所有四个侧面都有 png 阴影。
由于我在整个网站上都使用此布局,因此我想将一个变量传递给指定阴影 div 宽度的布局。我尝试在代码块中使用内容:
# In app/views/sessions/new.html.erb
<% render :layout => 'home/shadow_box' do %>
<% content_for :box_width %>640<% end %>
#... code for sign in form here
<% end %>
# In app/views/home/_shadow_box.html.erb
<div class="shadow-one" style="width:<%= yield :box_width %>;">
<div class="corner-a"></div>
<div class="corner-b"></div>
<div class="shadow-two">
<div class="shadow-three">
<div class="shadow-four">
<%= yield %>
</div>
</div>
</div>
</div>
这不起作用,反而导致整个代码块的双重渲染。
解决这个问题的最佳方法是什么?
In one of my views I apply a layout to a block of code:
# In app/views/sessions/new.html.erb
<% render :layout => 'home/shadow_box' do %>
#... code for sign in form here
<% end %>
The layout is a div that has png shadows on all four sides.
Since I use this layout all over my site, I want to pass a variable to the layout that specifies the width of the shadowed div. I tried using content for in the code block:
# In app/views/sessions/new.html.erb
<% render :layout => 'home/shadow_box' do %>
<% content_for :box_width %>640<% end %>
#... code for sign in form here
<% end %>
# In app/views/home/_shadow_box.html.erb
<div class="shadow-one" style="width:<%= yield :box_width %>;">
<div class="corner-a"></div>
<div class="corner-b"></div>
<div class="shadow-two">
<div class="shadow-three">
<div class="shadow-four">
<%= yield %>
</div>
</div>
</div>
</div>
This didn't work and instead resulted in a double render of the entire code block.
What's the best way to tackle this problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
想通了。
来自 API:“您还可以在一个布局中多次生成并使用块参数来区分各个部分。”
解决方案:
Figured it out.
From the API: "You can also yield multiple times in one layout and use block arguments to differentiate the sections."
Solution:
首先,您需要了解
布局
和部分
之间的区别。部分通常来自视图,但如果您使用 ajax,也可以从控制器使用。布局几乎总是在控制器中使用。首先在共享文件夹(例如 application/)中创建一个文件,然后在此文件夹中放置一个文件,无论您想要什么名称,都可以将其命名为任何名称,但它将包含您想要在整个站点中包含的材料。然后,当您将变量传递给分部时,它会在分部中作为局部变量进行调用。另外,对于部分,您不需要说
render :partial =>
您只需输入render 'application/some_file'
所以从您想要的视图来看:
然后从文件夹中的部分内容(例如
application/your_file.html.erb
)执行以下操作:First you need to know the difference between
layouts
andpartials
. Partials are generally from the view but can also be used from the controller if you are using ajax. Layouts are almost always used in the controller.First create a file in a shared folder such as application/ and in this folder put a file call it whatever you want but it will contain the material that you want to include all over your site. Then when you pass a variable to a partial it's called in the partial as a local variable. Also with partials you don't need to say
render :partial =>
you just putrender 'application/some_file'
So from the view you want this:
And then from the partial in the folder such as
application/your_file.html.erb
do this: