Topic: Better way to write conditional formatting

This code works fine, but is distracting me as I'm sure there must be a cleaner way to write it (it reminds me too much of asp!). Any ideas?

    <td><% if listing.count == 0 then %>0<% else %><strong><%= listing.count %></strong><% end %></td>

Also does it make sense to html encode strings before storing them in the database? Surely this would save processing later?

Thanks

Last edited by aubergene (2006-11-30 16:33:19)

Re: Better way to write conditional formatting

aubergene wrote:

This code works fine, but is distracting me as I'm sure there must be a cleaner way to write it (it reminds me too much of asp!). Any ideas?

<td><% if listing.count == 0 then %>0<% else %><strong><%= listing.count %></strong><% end %></td>

Maybe this:

<%= listing.count.zero? ? listing.count : content_tag(:strong, listing.count) %>

Or this:

<td><%= conditional_tag :strong, listing.count, !list.count.zero? %></td>

# in helper
def conditional_tag(tag, content, condition = true)
  condition ? content_tag(tag, content) : content
end


aubergene wrote:

Also does it make sense to html encode strings before storing them in the database? Surely this would save processing later?

It would save some processing later, yes, but I still prefer not to. I imagine things can easily get messy and out of hand. I've honestly never tried it so perhaps I shouldn't say anything though.

Railscasts - Free Ruby on Rails Screencasts