The direct way to do what you want is to use the capture_haml
helper to capture a block into a string to reuse later:
- buttons = case params[:action]
- when 'edit'
- capture_haml do
%a.property-save{ href: "#" }
%i.icon-save.icon-2x
= t('.property_save')
...
- when 'show'
- capture_haml do
...
- else
- capture_haml do
%a.property-save{ href: "#" }
%i.icon-open.icon-2x
= t('.property_open')
.my-butons
= buttons
You do seem to have a lot of repetition in the block, a better way might be something like:
.my-buttons
%a.property-save{ href: "#" }
%i.icon-2x{:class => "icon-#{params[:action]}"}
= t(".property_#{params[:action]}")
You’d have to adapt this to your real data, but note the value of the class
attribute will be formed by merging the icon-2x
and the result of the hash, so you can avoid some duplication.