LoginSignup
7
3

More than 5 years have passed since last update.

Hamlで条件に合う時だけ特定のクラスを加えたい

Last updated at Posted at 2015-12-16

Padrinoでwebアプリを書いている。Template engineにはhamlを使っているが、特定の条件の時だけ特定のクラスを付与したいようなことがある。例えばある期間以外ではリンクを無効にしておきたいなど。

- if some_condition
  %a.wrapper.disabled{href: "some_URI"}
    %span.content Some Content
- else
  %a.wrapper{href: "some_URI"}
    %span.content Some Content

DRYでない。Hamlではruby構文のスコープとタグのスコープが均しくインデントで制御されているのでちょっと面倒だった。試してみた所、{}の中で宣言したclassと外で宣言したclassはマージされるらしいので、こう書くとDRYになる。

%a.wrapper{href: "some_URI", class: [some_condition ? "disabled" : nil]}
  %span.content Some Content
7
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
3