Can you interpolate ruby variables within HAML css?
Asked Answered
B

2

12

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}");
    }
Buskus answered 11/7, 2011 at 21:36 Comment(0)
B
14

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;
  }
Buskus answered 11/7, 2011 at 22:35 Comment(0)
E
0
:css
  .mod_#{extra.id}_ { 
    background-image: url("#{extra.image}");
    height: #{ruby_height}#{measure_unit_ruby_variable};
  }
Enunciate answered 14/9, 2019 at 5:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.