I created these two helpers to make using placeholder a little bit easier.
def placeholder(message = 'Nothing to display', options = {}, &proc) # set default options o = { :class => 'placeholder', :tag => 'p' }.merge(options) # wrap the results of the supplied block, or # just print out the message if proc t = o.delete(:tag) concat tag(t, o, true), proc.binding yield concat "</#{t}>", proc.binding else content_tag o.delete(:tag), message, o end end def placeholder_unless(condition, *args, &proc) condition ? proc.call : concat(placeholder(args), proc.binding) end
Now you can use it as follows:
<%= placeholder :message => 'Nothing found' %> <% placeholder do %> Nothing found. <% end %>
Results in:
<p class="placeholder">Nothing found</p>
The second function allows the following usage:
<% placeholder_unless @posts.any?, 'No posts found' do %> <%= render :partial => @post %> <% end %>
The point is it all results in a nice, painless message with consistent markup and styling and I find it makes my code a bit more readable.
I'm sure the code could be optimized but for now it works.
Probably not the best solution, but any solution that uses the ternary operator is a good one.