I think odin's suggestion to use a partial is probably the best in most situations.
However, as an alternate solution, I found a thread where Nathan Weizenbaum suggested defining this method:
def haml_tag_if(condition, *args, &block)
if condition
haml_tag *args, &block
else
yield
end
end
Whatever is in the block would always be rendered, but the wrapping tag would appear or not based on the condition.
You would use it as follows:
- haml_tag_if(planning_to_mail?, :div, :id => 'envelope') do
%p I'm a letter
If planning_to_mail?
evaluates true
, you'd get:
<div id="envelope">
<p>I'm a letter</p>
</div>
If it evaluates false
, you'd get:
<p>I'm a letter</p>
He floated the idea of adding this to Haml::Helpers
, but that doesn't appear to have happened yet.