且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Rails 中的 render 和 yield 有什么区别

更新时间:2023-11-26 15:48:46

首先,yield 是 ruby​​,render 是 rails.通常对应用程序使用通用布局,其内部内容根据操作/上下文而变化.问题通常在于定义我们的布局结束和上下文特定模板的开始位置.例如,HTML 标题标签.假设您有一个名为 Cities 的应用程序.在大多数情况下,您希望页面标题始终为城市".但是,例如,如果您在阿姆斯特丹页面内,那么您希望将阿姆斯特丹"作为页面标题.

# application.html.erb<头><%= content_for?(:page_title) ?产量(:page_title):城市"%>......# city/index.html.erb<% content_for :page_title do %><%结束%><div class="bla"...

在 Rails 中,您通常在应用程序布局中定义应用程序标题.更改页面标题的一种策略是在特定城市模板中使用 content_for 并进行相应更改.

另一方面,Render 完成不同的渲染策略.直的.当您调用 render 时,它会进行渲染.content_for/yield 不会自动渲染,它存储在某处,然后在适当的位置填充缺失的位置.因此,与渲染相比,您可以将其更多地视为存储/搜索/替换",而渲染只是简单的渲染.

一个优于另一个的良好经验法则是:如果您正在编写的模板需要针对每个上下文呈现不同的信息,请强烈考虑使用 content_for.

Can someone explain the difference between "<%= render %>" and "<%= yield %> with <% content_for :partial do %>/<% end %>"? specifically how the routing changes when switching from one to another, the benefits of using one over the other, when is it practical to use one over the other. THIS is the closest explanation I have found, but isn't quite clear enough for me.

I have been trying for several days to wrap my head around this, but it seems that each configuration I try either comes close, or errors out.

If theres are three views, aaa and bbb and ccc, and each has an index.html.erb, but bbb and ccc have a _content.html.erb partial (signified by the underscore) how can you accomplish getting the bbb or ccc partial in aaa using either render or yield?

The following works:

aaa's index.html.erb :

<div">
  <%= render 'bbb/content' %>
</div>

and bbbs _content.html/erb :

<p>Content from bbb.</p>  

BUT this does NOT:

aaa's index.html.erb :

<div">
  <%= yield :container %>
</div>

and bbbs _content.html/erb :

<% content_for :container do %>
  <p>Content from bbb.</p>   ### viewed in aaa
<% end>

and cccs _content.html.erb would have nothing, or the content_for, but I still dont get aaa's index.html to be populated with content.

If I use the render, I can explicitly place the content in. But I thought that the benefit of using the yield :whatever would allow me to choose what to populate it with, and I can't get it to populate anything as soon as I change it from render to yield. Do I also have to update the routes file? If so, how do I choose which one to populate it with? Does that mean its in the controller? and needs an action?

I also have though that it depends on which file is initially routed to, but like I said, I think I need to understand the difference between the two before I can begin to use the partials to my advantage.

First of all, yield is ruby, render is rails. Usually one uses a common layout for the application whose inner content changes according to action/context. The problem usually lies in defining where our layout ends and context-specific template begins. Take, for instance, the HTML title tag. Let's say you have an application called Cities. In most cases, you want your page title to be "Cities" all the time. But, if you're for instance, inside Amsterdam page, then you would like the have "Amsterdam" as your page title.

# application.html.erb
<html>
  <head>
    <%= content_for?(:page_title) ? yield(:page_title) : "Cities" %>
    ......

# city/index.html.erb
<% content_for :page_title do %>
  <%= @city.name %> 
<% end %>

<div class="bla"...

Within Rails you usually define your application title in your application layout. One strategy for changing the page title would be to use content_for in the specific cities template and change accordingly.

Render, on the other hand, accomplishes different rendering strategies. Straight. When you call render, it renders. content_for/yield doesn't render automatically, it is stored somewhere and then fills up the missing spots in the due places. So, you can think of it as more as a "store/search/replace" in comparison to render, which just plain renders.

Good rule of thumb to use one over the other is: if the template you are writing needs to present different information per context, strongly consider using content_for.