From eed68fdbaf39807116f06f3a38eaec5b72e1552e Mon Sep 17 00:00:00 2001 From: jbranchaud Date: Mon, 11 May 2015 19:39:26 -0500 Subject: [PATCH] Add Conditional Class Selectors in Haml as a rails til. --- README.md | 1 + rails/conditional-class-selectors-in-haml.md | 21 ++++++++++++++++++++ 2 files changed, 22 insertions(+) create mode 100644 rails/conditional-class-selectors-in-haml.md diff --git a/README.md b/README.md index b18919d..7686456 100644 --- a/README.md +++ b/README.md @@ -53,6 +53,7 @@ smart people at [Hashrocket](http://hashrocket.com/). - [Attribute Getter without the Recursion](rails/attribute-getter-without-the-recursion.md) - [Attribute Was](rails/attribute-was.md) - [Capybara Page Status Code](rails/capybara-page-status-code.md) +- [Conditional Class Selectors in Haml](rails/conditional-class-selectors-in-haml.md) - [Creating Records of Has_One Associations](rails/creating-records-of-has-one-associations.md) - [Show Pending Migrations](rails/show-pending-migrations.md) diff --git a/rails/conditional-class-selectors-in-haml.md b/rails/conditional-class-selectors-in-haml.md new file mode 100644 index 0000000..91fb224 --- /dev/null +++ b/rails/conditional-class-selectors-in-haml.md @@ -0,0 +1,21 @@ +# Conditional Class Selectors in Haml + +You can assign a class selector to a tag in HAML like so: + +```ruby +%div.active +``` + +You can conditionally assign a class selector in a concise manner like so: + +```ruby +%div{ class: ( "active" if @thing.active? ) } +``` + +You can do multiple conditional class selectors with array syntax: + +```ruby +%div{ class: [ ("active" if @thing.active?), ("highlight" if @thing.important?) ] } +``` + +[source](http://stackoverflow.com/questions/3453560/append-class-if-condition-is-true-in-haml-with-rails)