1

I want to add some css style that I calculate in my ruby code within a haml template.

I understand that I should use the :css filter to add a style tag for my css like this:

%head
  - my_color = "#0000ff"
  :css
    .my-class { 
      / how can I set my_color here:
      background-color: red; 
    }

But how can I use my_color inside the filter?

Edit: To be clear, I am looking for a <style> tag, not inline css on an element.

1
  • Do you mean a <style> tag? Commented Dec 13, 2021 at 10:48

1 Answer 1

3

You can interpolate the variable with #{my_color}:

- my_color = "#0000ff"
:css
 .my-class {
   background-color: #{my_color};
 }
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks – so the block under :css is just regarded as a string from ruby's vantage point.
Not really. :css is an example of a filter in Haml. "The colon character designates a filter. This allows you to pass an indented block of text as input to another filtering program and add the result to the output of Haml.". Here you're doing variable interpolation (in the Haml parser) before passing the string into the filter. haml.info/docs/yardoc/file.REFERENCE.html#filters

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.