Hash with parent value and children.

30 views
Skip to first unread message

Pierre MARTIN

unread,
Mar 10, 2015, 11:40:10 AM3/10/15
to ha...@googlegroups.com
Hello,

I've been using HAML extensively for a very long time, but am puzzled by something and i finally decided to ask.

Let's say i want this:
<a ng-repeat='foo' ng-class='bar'>Hi there!</a>
This is easily translatable to HAML like this:
%a{ ng: { repeat: 'foo', class: 'bar' } } Hi!

However, I find myself in an awkward situation when i have to translate something like this:
<a ui-sref='foo' ui-sref-active='bar'>Hi!</a>
The only solution i have found so far is this, and i'm really not happy with it:
%a{ 'ui-sref'=>'foo', 'ui-sref-active'=>'bar' } Hi!

How would one model this very example with clean hashy syntax using HAML please? Is there an way to tell HAML to use something as our "anonymous" hash top-level value, eg:
%a{ ui: { sref: { special_sym: 'foo', active: 'bar' } } } Hi!

I have tried this:
%a{ ui: { sref: { ''=>'foo', active: 'bar' } } } Hi!
But it generates this:
<a ui-sref-='foo', ui-sref-active='bar'>Hi!</a>

How do you guys accommodate with this problem? Is there anything wrong in my approach / why?

Thanks!
Pierre.

Matt Wildig

unread,
Mar 10, 2015, 1:32:29 PM3/10/15
to ha...@googlegroups.com
On 08/03/2015 12:21, Pierre MARTIN wrote:
> However, I find myself in an awkward situation when i have to translate
> something like this:
> <a ui-sref='foo' ui-sref-active='bar'>Hi!</a>
> The only solution i have found so far is this, and i'm really not happy
> with it:
> %a{ 'ui-sref'=>'foo', 'ui-sref-active'=>'bar' } Hi!
>
> How would one model this very example with clean hashy syntax using HAML
> please? Is there an way to tell HAML to use something as our "anonymous"
> hash top-level value, eg:
> %a{ ui: { sref: { special_sym: 'foo', active: 'bar' } } } Hi!
>
> I have tried this:
> %a{ ui: { sref: { ''=>'foo', active: 'bar' } } } Hi!
> But it generates this:
> <a ui-sref-='foo', ui-sref-active='bar'>Hi!</a>
>
> How do you guys accommodate with this problem? Is there anything wrong in
> my approach / why?

I think you’ve found a weakness in the current behaviour, I can’t see a better way than how you’re currently doing this.

Perhaps we could have a special case for the empty string as a key, so that Haml doesn’t add the trailing '-', so that your last example would work.

This is probably worth opening as bug.


Matt

engineerDave

unread,
Mar 10, 2015, 2:21:39 PM3/10/15
to ha...@googlegroups.com
I think you've stumbled into a quagmire there with the changes to hashes in ruby 1.9

Personally I avoid nested hashes in haml and just use string keys and => syntax. 


    %a{ 'ui-sref' => 'foo', 'ui-sref-active' => 'bar' } Hi!
Reply all
Reply to author
Forward
0 new messages