module Nitro
# A helper mixin for programmatically building XHTML
# blocks.
module XhtmlMixin
# Render select options. The parameter is a hash of options.
#
# [+labels+]
# The option labels.
#
# [+values+]
# The corresponding values.
#
# [+selected+]
# The value of the selected option.
#
# === Examples
#
# labels = ['Male', 'Female']
# o.select(:name => 'sex') {
# o.options(:labels => labels, :selected => 1)
# }
#
# or
#
# #{options :labels => labels, :values => [..], :selected => 1}
# #{build :options, :labels => labels, :values => [..], :selected => 1}
def options(options = {})
if labels = options[:labels]
str = ''
unless values = options[:values]
values = (0...labels.size).to_a
end
selected = (options[:selected] || -1).to_i
labels.each_with_index do |label, idx|
value = values[idx]
if value == selected
str << %||
else
str << %||
end
end
return str
else
raise ArgumentError.new('No labels provided')
end
end
# Render a submit input.
def submit(options = nil)
str = ''
if options
opts = options.collect { |k, v| %[#{k}="#{v}"] }.join(' ')
str << %[]
else
str << %||
end
return str
end
end
end
# * George Moschovitis