Passing a hash to HAML brackets
I have a HAML line:
%button.navbar-toggle.collapsed{ type: :button, 'data-toggle' => :collapse, 'data-target' => "#bs-example-navbar-collapse-1" }
which is pretty ugly. I would like to be able to just pass everything between the parentheses as a ruby variable to keep things simpler.
Is there a way to do this? that is, I would prefer:
%button.navbar-toggle.collapsed{ options_hash }
+3
source to share
1 answer
If you are worried about the syntax 1.8
(this ugly one hashrockets
), you can do something like this:
%button.navbar-toggle.collapsed{ type: :button, data: { toggle: :collapse, target: "#bs-example-navbar-collapse-1" } }
It's at least a little better, isn't it? Or you can even try this, which also looks better than the old hash syntax:
%button.navbar-toggle.collapsed(type="button" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1")
But that is not what you asked for. Yes it is possible. You tried? I'm not really sure why you want to do this, but it should be possible:
- options_hash = { type: :button, data: { toggle: :collapse, target: "#bs-example-navbar-collapse-1" } }
%button.navbar-toggle.collapsed{ options_hash }
+2
source to share