module Sinatra::TagHelpers::HelperMethods

Public Instance Methods

checkbox_for(param, checked_if = false, attributes = {}) click to toggle source

checkbox_for creates an input of type checkbox with a ‘checked_if` argument to determine if it should be checked

<%= checkbox_for 'is_cool', User.is_cool? %>

Yields:

<input type='checkbox' name='is_cool' id='is_cool' value='true'>

Which will be marked with ‘checked` if `User.is_cool?` evaluates to true

# File lib/sinatra/tag-helpers.rb, line 96
def checkbox_for(param, checked_if = false, attributes = {})
  attributes = {
    :type  => 'checkbox',
    :value => 'true'
  }.merge(attributes)

  if checked_if || param_value(param) == attributes[:value].to_s
    attributes.merge!({ checked: true })
  end

  input_for param, attributes
end
days_for(param, attributes = {}) click to toggle source
# File lib/sinatra/tag-helpers.rb, line 209
def days_for(param, attributes = {})
  options = { 'Day' => '' }

  (1..31).each do |day|
    options[day] = day
  end

  select_for(param, options, attributes)
end
h(text = '')
Alias for: html_escape
html_escape(text = '') click to toggle source

don’t even try it

# File lib/sinatra/tag-helpers.rb, line 9
def html_escape(text = '')
  EscapeUtils.escape_html(text || '')
end
Also aliased as: h
input_for(param, attributes = {}) click to toggle source

input_for creates an <input> tag with a number of configurable options

if `param` is set in the `params` hash, the values from the `params` hash will be populated in the tag.

<%= input_for 'something_hidden', type: 'hidden', value: 'Shhhhhh' %>

Yields:

<input type='hidden' name='something_hidden' id='something_hidden' value='Shhhhhh'>
# File lib/sinatra/tag-helpers.rb, line 62
def input_for(param, attributes = {})
  attributes = {
    :type  => 'text',
    :value => param_value(param),
    :name  => param,
    :id    => param
  }.merge(attributes)

  "<input #{ to_attributes(attributes) }>"
end
months_for(param, attributes = {}) click to toggle source

shortcut to generate a month list

# File lib/sinatra/tag-helpers.rb, line 187
def months_for(param, attributes = {})
  select_for(param, settings.months_hash, attributes)
end
option_for(param, attributes = {}) click to toggle source

option_for creates an <option> element with the specified attributes

if the param specified is set to the value of this option tag then it is marked as 'selected'
designed to be used within a <select> element

<%= option_for 'turtles', key: 'I love them', value: 'love' %>

Yields:

<option value='love'>I love them</option>

If params is set to ‘love’ this yields:

<option value='love' selected>I love them</option>
# File lib/sinatra/tag-helpers.rb, line 133
def option_for(param, attributes = {})
  default = attributes.delete(:default).to_s

  if !params[param.to_sym].nil? && !params[param.to_sym].empty?
    default = param_value(param)
  end

  attributes.merge!({ :selected => true }) if default == attributes[:value].to_s
  key = attributes.delete(:key)

  "<option #{ to_attributes(attributes) }>#{ key }</option>"
end
param_value(param_name) click to toggle source

mostly so we can override this if we need to look anywhere besides the params

  • cough * session * cough *

# File lib/sinatra/tag-helpers.rb, line 30
def param_value(param_name)
  html_escape(params[param_name.to_sym] || '')
end
radio_for(param, attributes = {}) click to toggle source

radio_for creates an input tag of type radio and marks it ‘checked` if the param argument is set to the same value in the `params` hash

# File lib/sinatra/tag-helpers.rb, line 74
def radio_for(param, attributes = {})
  attributes = {
    :type => 'radio'
  }.merge(attributes)

  if param_value(param) == attributes[:value].to_s
    attributes.merge!({ :checked => true })
  end

  input_for param, attributes
end
select_for(param, options, attributes = {}) click to toggle source

select_for creates a <select> element with the specified attributes

options are the available <option> tags within the <select> box

<%= select_for 'days', { monday: 'Monday', myday: 'MY DAY!' } %>

Yields:

<select name='days' id='days' size='1'>
  <option value='monday'>Monday</option>
  <option value='myday'>MY DAY!</option>
</select>
# File lib/sinatra/tag-helpers.rb, line 158
def select_for(param, options, attributes = {})
  raise ArgumentError, "Options for `select_for` should be a Hash." unless options.is_a?(Hash)

  attributes = {
    :name => param,
    :id   => param
  }.merge(attributes)

  select = ["<select #{ to_attributes(attributes) }>"]

  options.collect do |key, val|
    # groups...
    if val.is_a?(Hash)
      select.push "<optgroup label='#{ key }'>"

      val.each do |group_key, group_val|
        select.push option_for(param, :key => group_key, :value => group_val, :default => attributes[:default] || param_value(param))
      end

      select.push "</optgroup>"
    else
      select.push option_for(param, :key => key, :value => val, :default => attributes[:default] || param_value(param))
    end
  end

  select.push('</select>').join(' ').chomp
end
textarea_for(param, attributes = {}) click to toggle source

creates a simple <textarea> tag

# File lib/sinatra/tag-helpers.rb, line 110
def textarea_for(param, attributes = {})
  attributes = {
    :name => param,
    :id   => param
  }.merge(attributes)

  "<textarea #{ to_attributes(attributes) }>#{ param_value(param) }</textarea>"
end
to_attributes(hash) click to toggle source

converts a hash into HTML style attributes

# File lib/sinatra/tag-helpers.rb, line 15
def to_attributes(hash)
  hash.collect do |key, value|
    # for things like data: { stuff: 'hey' }
    if value.is_a? Hash
      value.collect do |k, v|
        "#{key}-#{k}=\"#{v}\""
      end
    else
      value.is_a?(TrueClass) ? key.to_s : "#{key}=\"#{value}\""
    end
  end.join(' ').chomp
end
years_for(param, range = settings.years_range, attributes = {}) click to toggle source
# File lib/sinatra/tag-helpers.rb, line 191
def years_for(param, range = settings.years_range, attributes = {})
  options = { 'Year' => '' }

  if range.last > range.first
    # top down
    range.last.downto(range.first) do |element|
      options[element] = element
    end
  else
    # bottom up
    range.last.upto(range.first) do |element|
      options[element] = element
    end
  end

  select_for(param, options, attributes)
end