Last Updated: February 25, 2016
·
1.701K
· scarfacedeb

Conditional wrapper tag helper

It wraps given block into the tag when condition is true, and returns unwrapped block otherwise.

def conditional_tag(name, condition, options=nil, &block)
  if condition
    content_tag name, capture(&block), options
  else
    capture(&block)
  end
 end

Usage:

<%= conditional_tag :div, controller_name == "home", class: "above-fancy" do %>
  <header class="site-header">
    <%= primary_navigation %>
  </header>
<% end -%>

It wraps <header> in <div class="above-fancy"> only when controller_name == "home" is true