Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

slim dynamic conditional class [closed]

Just to help other developers, because there is no similar question on SO.

div class=(is_active? ? 'active' : 'inactive') div class=('active' if is_active?) 
like image 285
Sergey Alekseev Avatar asked Mar 06 '13 08:03

Sergey Alekseev


People also ask

What is Slim in ruby?

Slim is a template language whose goal is reduce the syntax to the essential parts without becoming cryptic.

What are .slim files?

Definition of slim file : a file very narrow in proportion to its length.

How do I comment in slim file?

HTML comment /!Use the forward slash immediately followed by an exclamation mark for html comments ( <! -- ... --> ).

What is Slim CSS?

Slim is a page-templating language that minimizes markup and syntax. It removes most of the extra "programming-like" symbols from HTML so that your code looks cleaner. Slim also adds if-else statements, loops, includes, and more.


2 Answers

See the examples below:

div class=(is_active? ? 'active' : 'inactive') div class=('active' if is_active?) 

The same approach can be used to assign dynamic values to other attributes.

like image 185
Sergey Alekseev Avatar answered Oct 07 '22 01:10

Sergey Alekseev


I use array of classes and nil element if there is no need to include class in list, then compact array to remove nil elements and finally join all together.

div class=(["cday", "col-md-1", day.day == 1 ? "col-md-offset-#{day.cwday-1}" : nil].compact.join(' ')) 
like image 23
Oleg Kr Avatar answered Oct 07 '22 02:10

Oleg Kr