3

I have a line of HAML:

%button.navbar-toggle.collapsed{ type: :button, 'data-toggle' => :collapse, 'data-target' => "#bs-example-navbar-collapse-1" }

that is quite ugly. I'd like to be able to just pass everything between the brackets as a ruby variable to simplify it.

Is there a way to do that? i.e. I'd rather have:

%button.navbar-toggle.collapsed{ options_hash }
ThomYorkkke
  • 2,061
  • 3
  • 18
  • 26

1 Answers1

3

If you are concerned about the 1.8 syntax (those ugly hashrockets), you could do something like this:

%button.navbar-toggle.collapsed{ type: :button, data: { toggle: :collapse, target: "#bs-example-navbar-collapse-1" } }

That's at least a bit better, right? Or you could 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's not what you asked. Yeah, it's possible. Have you tried? I'm not entirely sure why you would want to do this, but this should be possible:

- options_hash = { type: :button, data: { toggle: :collapse, target: "#bs-example-navbar-collapse-1" } }
%button.navbar-toggle.collapsed{ options_hash }
Kevin
  • 1,213
  • 10
  • 13