Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

block_given? always returns true in erb templates

In Rails 5.2.3, I need to render a partial which takes an optional block.

# users/_user.html.erb
...
<% if block_given? %>
  <%= yield %>
<% else %>
  <h1>Goodbye world</h1>
<% end %>
...

However block_given? returns true regardless of which version I choose to go with:


<%# Version 1 - block_given? returns true %>
<%= render partial: "users/_user" do %>
  <h1>hello world</h1>
<% end %>

<%# Version 2 - block_given? also returns true %>
<%= render partial: "users/_user" %>

What's going on here and why is this happening?

like image 558
stratis Avatar asked Oct 22 '19 19:10

stratis


2 Answers

Because all Rails templates support content_for :xyz, which is triggered by yield :xyz, it means all templates are always wrapped in a block that is prepared to fetch this content_for data.

Because this pre-programmed block is always there in order to accommodate content_for, it means block_given? will always return true.

I think this may actually be a small oversight in the Rails view design. It would be nice if we'd have a separate method to detect if a partial was supplied a block.

One idea for workaround:

<% if (block = yield).empty? %>
  <h1>Goodbye world</h1>
<% else %>
  <%= block %>
<% end %>
like image 116
Casper Avatar answered Nov 09 '22 22:11

Casper


While being clever and a generic solution, I'm not a fan of the (block = yield).empty? in that particular instance.

In my use case and this one, where the default content is so simple, I prefer this approach:

<%= yield.presence || content_tag(:h1, "Goodby world") %>
like image 22
Arnaud Avatar answered Nov 09 '22 23:11

Arnaud