Module: Sinatra::IfHelpers

Defined in:
lib/sinatra/support/ifhelpers.rb

Overview

Useful HAML condition helpers.

require 'sinatra/support/htmlhelpers'

class Main < Sinatra::Base
  helpers Sinatra::IfHelpers
end

Helpers

These are helpers you can use in HAML files.

active_if - Adds class=active if a condition passes.

- @users.each do |user|
  %li{active_if(user == current_user)}
    = user.to_s

checked_if - Adds checked=1.

%input{checked_if(page.available?), type: 'checkbox'}

hide_if - Adds style=display:none.

%div#comments{hide_if(post.comments.empty?)}

show_if - Inverse of hide_if.

selected_if - Adds selected=1.

disabled_if - Adds disabled=1.

enabled_if - Inverse of disabled_if.

Instance Method Summary collapse

Instance Method Details

#active_if(condition) ⇒ Object



36
37
38
# File 'lib/sinatra/support/ifhelpers.rb', line 36

def active_if(condition)
  condition ? {:class => 'active'} : {}
end

#checked_if(condition) ⇒ Object



40
41
42
# File 'lib/sinatra/support/ifhelpers.rb', line 40

def checked_if(condition)
  condition ? {:checked => '1'} : {}
end

#disabled_if(condition) ⇒ Object



48
49
50
# File 'lib/sinatra/support/ifhelpers.rb', line 48

def disabled_if(condition)
  condition ? {:disabled => '1'} : {}
end

#enabled_if(condition) ⇒ Object



52
53
54
# File 'lib/sinatra/support/ifhelpers.rb', line 52

def enabled_if(condition)
  disabled_if !condition
end

#hide_if(condition) ⇒ Object



56
57
58
# File 'lib/sinatra/support/ifhelpers.rb', line 56

def hide_if(condition)
  condition ? {:style => 'display:none'} : {}
end

#selected_if(condition) ⇒ Object



44
45
46
# File 'lib/sinatra/support/ifhelpers.rb', line 44

def selected_if(condition)
  condition ? {:selected => '1'} : {}
end

#show_if(condition) ⇒ Object



60
61
62
# File 'lib/sinatra/support/ifhelpers.rb', line 60

def show_if(condition)
  hide_if !condition
end