Rails 3: yield/content_for具有某些默认值?

时间:2022-07-07 23:31:12

Is there any way to detect if #content_for was actually applied to a yield scope in Rails?

是否有任何方法可以检测#content_for是否实际应用于Rails的yield范围?

A classic example being something like:

一个典型的例子是:

<title><%= yield :page_title %></title>

If a template doesn't set that with

如果模板没有设置它

<% content_for :page_title, "Something here" %>

Is there a way to have the layout put something else there instead?

是否有办法让布局放其他东西呢?

I tried defining it with #content_for in the layout itself, but this just causes the text to be doubled-up. I also tried:

我尝试在布局本身中使用#content_for来定义它,但这只会导致文本被双重化。我也试过:

<%= (yield :page_title) or default_page_title %>

Where #default_page_title is a view helper.

其中#default_page_title是一个视图帮助器。

This just left the block completely empty.

这让block完全空着。

4 个解决方案

#1


114  

You can use content_for? to check if there is content with a specific name:

您可以使用content_for吗?检查是否有特定名称的内容:

<% if content_for?(:page_title) %>
  <%= yield(:page_title) %>
<% else %>
  <%= default_page_title %>
<% end %>

or

<%= content_for?(:page_title) ? yield(:page_title) : default_page_title %>

Then in your views you can specify the content like

然后,可以在视图中指定内容

<% content_for :page_title do %>
    Awesome page
<% end %>

#2


11  

As of Rails 3, yield() returns empty string if there was no content for the requested key, so you can do something like this:

在Rails 3中,yield()返回空字符串,如果请求的键没有内容,那么您可以这样做:

<title><%= yield(:page_title).presence || 'Default Page Title' %></title>

In your application helper, if you define:

在您的应用程序助手中,如果您定义:

def page_title(title = nil)
  title ? content_for(:page_title) { title } : content_for(:page_title).presence
end

Then you can do something like this:

然后你可以这样做:

<title><%= page_title or 'Default Page Title' %></title>

And in your views you can customize with:

在您的视图中,您可以使用:

<% page_title 'My Page Title' %>

#3


5  

I have just released a new gem, dry_views, which provides enhanced content_for methods.

我刚刚发布了一个新的gem dry_views,它提供了增强的content_for方法。

The benefit over the accepted answer is it allows you to remove all logic (if/else) from your views.

与已接受的答案相比,它的好处是允许您从视图中删除所有逻辑(if/else)。

See the readme for extensive examples:

请参阅自述以了解更多的例子:

- no_content_for :key

- no_content_for:关键

  • Use a dash (-) not equals (=)
  • 使用dash(-)不等于(=)
  • Will prevent a later content_for from rendering. This allows template overrides of layouts.
  • 将防止稍后的content_for呈现。这允许模板覆盖布局。

= content_for_with_default :key

= content_for_with_default:关键

  • Use an equal (=) not a dash (-)
  • 使用等号(=)而不是破折号(-)
  • You provide it with the default HAML via a block or a set of params that are the same as you would pass to a standard "render" call (i.e. :partial => 'foo', :locals => {:bar => 'bar'}).`
  • 您通过一个块或一组params向它提供默认的HAML,这些参数与您将传递到标准的“呈现”调用相同(例如:partial => 'foo',:local => {:bar => 'bar'})。

= content_for :key

= content_for:关键

  • Use an equal (=) not a dash (-)
  • 使用等号(=)而不是破折号(-)
  • You provide it with a block, i.e. {render :partial => 'foo'}, and it will override content_for_with_default. It has the same precedence as no_content_for, so whichever is rendered first wins, so if a layout has either no_content_for or content_for (with or without default) the template can now override it.
  • 您提供了一个块,即{render:partial => 'foo'},它将重写content_for_with_default。它与no_content_for具有相同的优先级,因此无论首先呈现的是哪个,如果一个布局有no_content_for或content_for(有或没有默认),模板现在可以覆盖它。

#4


3  

Better answer for rails 3 here:

rails 3的更好答案是:

Yield and default case || do not output default case

良率和默认情况||不输出默认情况

<%= yield(:title).presence || 'My Default Title' %>

< % =产量(标题)。存在|| '我的默认标题' %>

#1


114  

You can use content_for? to check if there is content with a specific name:

您可以使用content_for吗?检查是否有特定名称的内容:

<% if content_for?(:page_title) %>
  <%= yield(:page_title) %>
<% else %>
  <%= default_page_title %>
<% end %>

or

<%= content_for?(:page_title) ? yield(:page_title) : default_page_title %>

Then in your views you can specify the content like

然后,可以在视图中指定内容

<% content_for :page_title do %>
    Awesome page
<% end %>

#2


11  

As of Rails 3, yield() returns empty string if there was no content for the requested key, so you can do something like this:

在Rails 3中,yield()返回空字符串,如果请求的键没有内容,那么您可以这样做:

<title><%= yield(:page_title).presence || 'Default Page Title' %></title>

In your application helper, if you define:

在您的应用程序助手中,如果您定义:

def page_title(title = nil)
  title ? content_for(:page_title) { title } : content_for(:page_title).presence
end

Then you can do something like this:

然后你可以这样做:

<title><%= page_title or 'Default Page Title' %></title>

And in your views you can customize with:

在您的视图中,您可以使用:

<% page_title 'My Page Title' %>

#3


5  

I have just released a new gem, dry_views, which provides enhanced content_for methods.

我刚刚发布了一个新的gem dry_views,它提供了增强的content_for方法。

The benefit over the accepted answer is it allows you to remove all logic (if/else) from your views.

与已接受的答案相比,它的好处是允许您从视图中删除所有逻辑(if/else)。

See the readme for extensive examples:

请参阅自述以了解更多的例子:

- no_content_for :key

- no_content_for:关键

  • Use a dash (-) not equals (=)
  • 使用dash(-)不等于(=)
  • Will prevent a later content_for from rendering. This allows template overrides of layouts.
  • 将防止稍后的content_for呈现。这允许模板覆盖布局。

= content_for_with_default :key

= content_for_with_default:关键

  • Use an equal (=) not a dash (-)
  • 使用等号(=)而不是破折号(-)
  • You provide it with the default HAML via a block or a set of params that are the same as you would pass to a standard "render" call (i.e. :partial => 'foo', :locals => {:bar => 'bar'}).`
  • 您通过一个块或一组params向它提供默认的HAML,这些参数与您将传递到标准的“呈现”调用相同(例如:partial => 'foo',:local => {:bar => 'bar'})。

= content_for :key

= content_for:关键

  • Use an equal (=) not a dash (-)
  • 使用等号(=)而不是破折号(-)
  • You provide it with a block, i.e. {render :partial => 'foo'}, and it will override content_for_with_default. It has the same precedence as no_content_for, so whichever is rendered first wins, so if a layout has either no_content_for or content_for (with or without default) the template can now override it.
  • 您提供了一个块,即{render:partial => 'foo'},它将重写content_for_with_default。它与no_content_for具有相同的优先级,因此无论首先呈现的是哪个,如果一个布局有no_content_for或content_for(有或没有默认),模板现在可以覆盖它。

#4


3  

Better answer for rails 3 here:

rails 3的更好答案是:

Yield and default case || do not output default case

良率和默认情况||不输出默认情况

<%= yield(:title).presence || 'My Default Title' %>

< % =产量(标题)。存在|| '我的默认标题' %>