你能在HAML CSS中插入Ruby变量吗?

12

我需要一系列的类,.module1、.module2、... module(n)。

同时我希望使用css、ruby和HAML来定义这些类:

:css
  .mod1 {
        background-image: url("#{extra.image}");
    }

是否有可能插值Ruby变量以节省工作?

.module.mod"#{extra.id}"
  %h3 #{extra.title}
  %p #{extra.description}
  %a.btn-default{:href => "#", :target => "_top"} enter now

:css
  .mod#{extra.id} {
        background-image: url("#{extra.image}");
    }
2个回答

14

根据我使用的HAML_REFERENCE,我采用了这种方法:

- flavor = "raspberry"
#content
  :textile
    I *really* prefer _#{h flavor}_ jam.

在:css之后插入变量

.module.modone{:class => "#{cycle("mod_#{extra.id}_")}"}

  %h3 #{extra.title}
  %p #{extra.description}
  %a.btn-default{:href => "#", :target => "_top"} enter now

:css
  .mod_#{extra.id}_ { 
    background-image: url("#{extra.image}");
    background-color: #4073DF;
  }

0
:css
  .mod_#{extra.id}_ { 
    background-image: url("#{extra.image}");
    height: #{ruby_height}#{measure_unit_ruby_variable};
  }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接