# Determining whether yields have content

**URL:** <https://www.ruby-forum.com/t/determining-whether-yields-have-content/156903>\
**Category:** Rails\
**Created:** [January 5, 2009, 4:19am UTC](https://www.ruby-forum.com/t/determining-whether-yields-have-content/156903 "2009-01-05T04:19:49Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![chero](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e99b99/32.png) [@chero](https://www.ruby-forum.com/u/chero)\
**Post date:** [January 5, 2009, 4:19am UTC](https://www.ruby-forum.com/t/determining-whether-yields-have-content/156903/1 "2009-01-05T04:19:49Z")

</div>

Hey All,  
I would like some logic that would help me determine whether a yield  
has  
any content to it. For example:

 \<%= yield :submenu %\>

I only want to display the div around the yield if the yield will have  
content.

Any ideas?

Thanks,  
Jim

---

<div class="post-metadata">

**Author:** ![chero](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e99b99/32.png) [@chero](https://www.ruby-forum.com/u/chero)\
**Post date:** [January 5, 2009, 5:48am UTC](https://www.ruby-forum.com/t/determining-whether-yields-have-content/156903/2 "2009-01-05T05:48:05Z")

</div>

Just confirming, its expensive since it renders the yield twice,  
correct?  
What if i did something like:

\<% yield\_contents = yield(:submenu)  
if !yield\_contents.strip.blank? -%\>

 \<%= yield\_contents %\> 
\<% end -%\>

Would that work?

I guess I never realized that I could yield twice. Thanks for the tip.  
Much  
appreciated.

---

<div class="post-metadata">

**Author:** ![chero](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e99b99/32.png) [@chero](https://www.ruby-forum.com/u/chero)\
**Post date:** [January 5, 2009, 4:54am UTC](https://www.ruby-forum.com/t/determining-whether-yields-have-content/156903/3 "2009-01-05T04:54:19Z")

</div>

On Jan 4, 2009, at 7:18 PM, James E. wrote:

> Any ideas?

You can do:

\<% if !yield(:submenu).strip.blank? -%\>

 \<%= yield :submenu %\> 
\<% end -%\>

The call may be expensive so you might want to create some other  
method of determining whether the submenu is likely to have content.  
But this should work.

HTH

---

<div class="post-metadata">

**Author:** ![chero](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e99b99/32.png) [@chero](https://www.ruby-forum.com/u/chero)\
**Post date:** [January 5, 2009, 5:53pm UTC](https://www.ruby-forum.com/t/determining-whether-yields-have-content/156903/4 "2009-01-05T17:53:17Z")

</div>

On Jan 4, 2009, at 8:47 PM, James E. wrote:

> Just confirming, its expensive since it renders the yield twice,  
> correct? What if i did something like:
> 
> \<% yield\_contents = yield(:submenu)  
> if !yield\_contents.strip.blank? -%\>
> 
> \<%= yield\_contents %\> 
> \<% end -%\>

That should work. A different solution altogether would be to have the  
submenu contain the div wrapper so you can make the test there and  
eliminate all the gnarly logic in your layout.
