# ================
# = Form Helpers =
# ================
module Sinatra
module FormTagHelpers
# input_for creates an 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:
#
#
#
def input_for(param, attributes = {})
# default values when not specified.
attributes = {
:type => 'text', # can be any HTML input type ('email', 'submit', 'password', etc.)
:value => h(params[param.to_sym]) || '',
:name => param,
:id => attributes[:id] || param
}.merge(attributes)
""
end
# 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
def radio_for(param, attributes = {})
attributes = {
:type => 'radio'
}.merge(attributes)
if params[param.to_sym].to_s == attributes[:value].to_s
attributes.merge!({ :checked => nil })
end
input_for param, attributes
end
# 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:
#
#
#
# Which will be marked with `checked` if `User.is_cool?` evaluates to true
#
def checkbox_for(param, checked_if, attributes = {})
attributes = {
:type => 'checkbox',
:value => 'true'
}.merge(attributes)
if checked_if || params[param.to_sym] == 'true'
attributes.merge!({ :checked => nil })
end
input_for param, attributes
end
# creates a simple