Ruby on Rails的“content_for:title”如何获得稍后分配的内容?

时间:2022-05-15 01:23:00

The short question is: how can a subpage's

简短的问题是:子页面怎么样

<% content_for :title do 'Showing product' end %>

set the :title for the main layout?

设置:主要布局的标题?


details:

细节:

We can use in the application layout application.html.erb

我们可以在应用程序布局application.html.erb中使用

<title><%= content_for :title %>
  ...
  <%= yield %>

and I think yield returns the content for a subpage, such as from show.html.erb, where it contains:

我认为yield会返回子页面的内容,例如show.html.erb,其中包含:

<% content_for :title do 'Showing product' end %>

How can the :title somehow get used by something above the yield? I thought the title part is evaluated first, and then the yield, so how can the :title retroactively set the content for the <title> tag?

如何:标题以某种方式被高于收益率的东西使用?我认为首先评估标题部分,然后评估yield,那么:title如何追溯设置

标签的内容?</p>

2 个解决方案

#1


8  

Short answer: By cheating.

简答:作弊。

Long answer: ActionView redefines yield so it is not the same yield we know and love from good ol' ruby. In fact the template file is rendered before the layout file and then the yield in the layout file will be substituted by the already rendered template. content_for blocks are saved into class variables and so you can later access them from your layout.

答案很长:ActionView重新定义了产量,因此它与我们所知道的产品不同,并且非常喜欢好的红宝石。实际上,模板文件在布局文件之前呈现,然后布局文件中的yield将被已呈现的模板替换。 content_for块保存在类变量中,因此您可以稍后从布局中访问它们。

#2


5  

I defined a helper method title in my application_helper.rb file like so:

我在application_helper.rb文件中定义了一个帮助方法标题,如下所示:

module ApplicationHelper
  def title(page_title)
    content_for(:title){ page_title }
    page_title
  end
end

Then at the top of my content ERB files I can do this

然后在我的内容ERB文件的顶部,我可以做到这一点

<% title "Rails Rocks" %>
Other regular content

And in the application.html.erb

并在application.html.erb中

<html>
<head>
  <% title = yield(:title).chop! %>
  <title><%= title || 'Default Title' %></title>
</head>
<body>
  <h1 class="title"><%= title %></h1>
</body>

#1


8  

Short answer: By cheating.

简答:作弊。

Long answer: ActionView redefines yield so it is not the same yield we know and love from good ol' ruby. In fact the template file is rendered before the layout file and then the yield in the layout file will be substituted by the already rendered template. content_for blocks are saved into class variables and so you can later access them from your layout.

答案很长:ActionView重新定义了产量,因此它与我们所知道的产品不同,并且非常喜欢好的红宝石。实际上,模板文件在布局文件之前呈现,然后布局文件中的yield将被已呈现的模板替换。 content_for块保存在类变量中,因此您可以稍后从布局中访问它们。

#2


5  

I defined a helper method title in my application_helper.rb file like so:

我在application_helper.rb文件中定义了一个帮助方法标题,如下所示:

module ApplicationHelper
  def title(page_title)
    content_for(:title){ page_title }
    page_title
  end
end

Then at the top of my content ERB files I can do this

然后在我的内容ERB文件的顶部,我可以做到这一点

<% title "Rails Rocks" %>
Other regular content

And in the application.html.erb

并在application.html.erb中

<html>
<head>
  <% title = yield(:title).chop! %>
  <title><%= title || 'Default Title' %></title>
</head>
<body>
  <h1 class="title"><%= title %></h1>
</body>