Can you interpolate ruby variables within HAML css?

倾然丶 夕夏残阳落幕 提交于 2019-12-22 03:48:07

问题


I need a series of classes, .module1, .module2, ... module(n).

I also want to define those classes using css, ruby and HAML:

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

Is it possible to interpolate ruby variables to save work?

.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}");
    }

回答1:


According to the HAML_REFERENCE I used this method:

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

to interpolate variables after :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;
  }



回答2:


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


来源:https://stackoverflow.com/questions/6656915/can-you-interpolate-ruby-variables-within-haml-css

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!