Render 与 Render Partial 和 Yield 之间的区别 [英] Difference between Render and Render Partial and Yield
问题描述
我从 Rails 指南中读过,看过 Micheal Hartel 的书,现在从 Rails View 中读过,但我仍然感到困惑:(
有一个 _footer.html.erb
文件,所以它是一个部分"文件,在它编写的代码中:
<%=render 'layouts/footer' %>
所以我的理解是,当它看到这一点时,会在此处插入页脚文件的 HTML.好的...现在几页后它说:
<%= 渲染部分:'activitiy_items/recent' %>
那么为什么这次我们在这里有部分"这个词,而在上一次没有呢?
在其他地方我看到 <%= yield :sidebar %>
所以这个 yield
也在它的位置插入了 HTML?嗯,这不是 render
正在做的吗?
我希望如果另一个程序员而不是书籍向我解释这一点,也许这次我明白了:)
render
&渲染部分:
render 'some_view'
是render partial: 'some_view'
的简写.render file: 'view'
将查找文件view.html.erb
而不是_view.html.erb
(.erb
或您使用的任何其他渲染器)render
不会接受额外的局部变量,你需要使用render partial:
如下:render partial: 'some/path/to/my/partial', locals: { custom_var: 'Hello' }
(http://guides.rubyonrails.org/layouts_and_rendering.html#passing-本地变量)
产量
&content_for
yield
通常用于布局.它告诉 Rails 将此块的内容放在布局中的那个位置.- 当您执行与
content_for :something
相关联的yield :something
时,您可以传递一个代码块(视图)来显示yield :something
被放置(见下面的例子).
一个关于收益的小例子:
在您的布局中:
<头><%=产量:html_head %>头部><身体><div id="侧边栏"><%=产量:侧边栏%>
在您看来:
<% content_for :sidebar do %>此内容将显示在侧边栏部分<%结束%><% content_for :html_head do %><script type="text/javascript">console.log("Hello World!");<%结束%>
这将生成以下 HTML:
<头><script type="text/javascript">console.log("Hello World!");头部><身体><div id="侧边栏">此内容将显示在侧边栏部分
<小时>
可能有帮助的帖子:
链接到文档和指南:
- http://guides.rubyonrails.org/layouts_and_rendering.html#passing-局部变量
- http://apidock.com/rails/ActionView/Helpers/CaptureHelper/content_for
- http://apidock.com/rails/ActionController/Base/render
I have read it from the Rails guides, Have looked at Micheal Hartel book and now reading it from Rails View book but still I get confused :(
There is a _footer.html.erb
file so it is a "partial" and in the code it has written:
<%=render 'layouts/footer' %>
so my understanding is that when it sees this, goes and insert the HTML for footer file in here. Ok... Now a few pages later it is saying:
<%= render partial: 'activitiy_items/recent' %>
so WHY this time we have the word "partial" in here but we didn't have it in the previous one?
And there somewhere else I see <%= yield :sidebar %>
So this yield
also insert HTML in its place? Well wasn't it what render
was doing?
I was hoping if another programmer instead of books explains this to me maybe I get it this time:)
render
& render partial:
render 'some_view'
is a shorthand forrender partial: 'some_view'
.render file: 'view'
will look for a fileview.html.erb
and NOT_view.html.erb
(.erb
or any other renderer you use)render
will not accept additional local variables for the partial, you need to userender partial:
as following for that:render partial: 'some/path/to/my/partial', locals: { custom_var: 'Hello' }
(http://guides.rubyonrails.org/layouts_and_rendering.html#passing-local-variables)
yield
& content_for
yield
is typically used in layouts. It tells Rails to put the content for this block at that place in the layout.- When you do
yield :something
associated withcontent_for :something
, you can pass a block of code (view) to display where theyield :something
is placed (see example below).
A small example about yield:
In your layout:
<html>
<head>
<%= yield :html_head %>
</head>
<body>
<div id="sidebar">
<%= yield :sidebar %>
</div>
</body>
In one of your view:
<% content_for :sidebar do %>
This content will show up in the sidebar section
<% end %>
<% content_for :html_head do %>
<script type="text/javascript">
console.log("Hello World!");
</script>
<% end %>
This will produce the following HTML:
<html>
<head>
<script type="text/javascript">
console.log("Hello World!");
</script>
</head>
<body>
<div id="sidebar">
This content will show up in the sidebar section
</div>
</body>
Posts that might help:
Links to documentation & guides:
- http://guides.rubyonrails.org/layouts_and_rendering.html#passing-local-variables
- http://apidock.com/rails/ActionView/Helpers/CaptureHelper/content_for
- http://apidock.com/rails/ActionController/Base/render
这篇关于Render 与 Render Partial 和 Yield 之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!