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

- (Object) active_if(condition)



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

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

- (Object) checked_if(condition)



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

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

- (Object) disabled_if(condition)



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

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

- (Object) enabled_if(condition)



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

def enabled_if(condition)
  disabled_if !condition
end

- (Object) hide_if(condition)



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

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

- (Object) selected_if(condition)



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

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

- (Object) show_if(condition)



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

def show_if(condition)
  hide_if !condition
end