Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Haml: how to generate dynamic id

I've got following code in erb which works fine:

<div id='flash_<%= flash.keys.first.to_s %>'>
    <%=h    flash[flash.keys.first] %>
</div>

I want to convert it into haml:

#flash_#{flash.keys.first.to_s}
  =h flash[flash.keys.first]

But I receive error:

Illegal element: classes and ids must have values.

Which is strange as there IS value, 'flash_' part is always present, I get that error even when I do:

#flash_#{nil.object_id}

Apparently something wrong is with my Ruby interpolation in haml, but I can't get it right. According to documentation http://haml.info/docs/yardoc/file.REFERENCE.html#ruby_interpolation_ #{} is used to interpolate Ruby and it works in such case:

#flash_
  #{flash.keys.first.to_s}

but that's not what I want.

To sum up, I want to get the following output:

<div id="flash_foo"> blahblah </div>

but it can be also:

<div id="flash_"></div>

How to obtain that with haml?

like image 580
zrl3dx Avatar asked Apr 28 '13 15:04

zrl3dx


People also ask

How do I give a Haml ID?

In Haml, we write a tag by using the percent sign and then the name of the tag. This works for %strong , %div , %body , %html ; any tag you want. Then, after the name of the tag is = , which tells Haml to evaluate Ruby code to the right and then print out the return value as the contents of the tag.

How to use Haml in rails?

To use Haml with Rails, simply add Haml to your Gemfile and run bundle . If you'd like to replace Rails's Erb-based generators with Haml, add haml-rails to your Gemfile as well.


1 Answers

%div{ :id => "flash_#{flash.keys.first}" }    
  =h flash[flash.keys.first]
like image 200
Reuben Mallaby Avatar answered Sep 20 '22 01:09

Reuben Mallaby