Haml 4.1 (currently in beta – it might be version 5 when it’s released) contains a haml_tag_if
method that will do what you want:
- haml_tag_if image['href'], :a, href: image['href'] do
some markup
This will produce either:
<a href='foo'>
some markup
</a>
or just
some markup
depending on whether image['href']
is truthy.
If you don’t want to upgrade yet, you can add the method yourself as a helper, see this answer to a similar question: https://stackoverflow.com/a/8595810/214790 (the code there is basically the same as the new code in Haml itself).