How to iterate over an array with HAML?

2019-06-22 06:54发布

问题:

I have this in a HAML layout (layout.haml)

- @fonts.each do |font|
  %link{:href=>"//fonts.googleapis.com/css?family={font}",:rel=>"stylesheet",:type=>"text/css"}

And I have this in the HAML template index.html.haml

- @fonts = ['Lato:400,300,100','Droid+Serif:700,400'];

When I compile, I get this:

<link href='//fonts.googleapis.com/css?family={font}' rel='stylesheet' type='text/css' />
<link href='//fonts.googleapis.com/css?family={font}' rel='stylesheet' type='text/css' />

What am I doing wrong?

回答1:

You forgot the hash symbol.

- @fonts.each do |font|
  %link{:href=>"//fonts.googleapis.com/css?family=#{font}",:rel=>"stylesheet",:type=>"text/css"}
                                                  ^ here