[RUBY] Use variables for class and id names in haml

There were times when I wanted to index the class name during application development, and I was confused about how to write it, so please refer to it if there is a similar person.

Basic

First, the basic class name id name is written starting with "." And "#".

#name of the class
.item_name
%p.item_name

#ID name
#item_name
%p#item_name

How to write using variables

And this time I stumbled upon how to write using variable names in each statement. It was applied in the following writing style!

[email protected]_with_index do |item, i|
  %div{class: "#{item.name}"}
    %p{class: "item_name_#{i}"}

Enclose the class name in {} and it will be applied! It is applied in the same way even if it is not a each statement

%div{class: @item.name}

Recommended Posts

Use variables for class and id names in haml
Use different TransactionManager for metadata and step in spring-batch
Use of Abstract Class and Interface properly in Java
Use ruby variables in javascript.
[Java] Make variables in extended for statement and for Each statement immutable
How to separate words in names in classes, methods, and variables
StringBuffer and StringBuilder Class in Java
java (use class type for field)
Getter names are different in lombok and eclipse for fields like `rPoint`
Do not use instance variables in partial
[Docker] Use environment variables in Nginx conf
[Java] for Each and sorted in Lambda
[Java] Differences between instance variables and class variables
Watch out for embedded variables in S2Dao
Proper use of interface and abstract class
Java programming (static clauses and "class variables")
How to use environment variables in RubyOnRails
[Java] How to use FileReader class and BufferedReader class
(Basic authentication) environment variables in rails and Docker
Criteria for proper use of render and redirect_to
Ruby: Differences between class methods and instance methods, class variables and instance variables
Use pagy for pagination in your Rails app.
Use Shift Right and Shift Left properly in Selenium
[Java] How to use Calendar class and Date class
I want to use PowerMock in a class that combines parameterized tests and ordinary tests