Hi.
I am writing an application that has a lot of boolean conditional
display logic, like this:
<% if user.description then %>
<%= user.description %>
<% end %>Often the displayed content is more complex than the above, and to clean
up my views I am trying to pull a lot of this sort of thing into
partials.
However, the problem arises that I will frequently need to format the
same information in different ways. For example, the above user
description appears as a standalone item so it can logically be wrapped
in a
tag. But it might be an inline item that should appear in a
, or a list item that should appear in
That means that the enclosing tag (and, typically, CSS class reference)
need to be pulled back out into the view, like:
But this will result in empty
or or
in the event that the conditional test (in the partial) returns false,
which happens frequently. So that suggests I should put the conditional
logic back in the view and remove it from the partial altogether:
<% if user.description then %>
<%= render :partial => "user_description" %>
<% end %>…which sort of defeats the purpose of the partial.
Is there any recognized best practice for handling this situation? What
would be handy is if the render method could take an :if parameter. Or I
suppose I could write a helper that forms a wrapper tag given a tag name
and class, and include the helper in lots of partials:
<%= render :partial => “user_description”, :locals => { :tag_name => li,
:class => “css_class” %>
But I’m open to better suggestions.
Thanks!
/afb