Module: N::XhtmlBuilderMixin

Included in:
XhtmlBuilder, XhtmlString
Defined in:
lib/nitro/builders/xhtml.rb

Overview

A helper mixin for programmatically building XHTML blocks. – TODO: add table generator, form generator ++

Instance Method Summary collapse

Instance Method Details

#options(opts, selected = nil) ⇒ Object

Render select options.

opts

The options to render, can be an Array or a Hash (allows you to explicitly set the value mapping).

selected

The value of the selected option.

Example

options = [‘Male’, ‘Female’] o.select(:name => ‘sex’) { o.options(options, selected = 1) }

or

options = { ‘Male’ => ‘m’, ‘Female’ => ‘f’ } o.select(:name => ‘sex’) { o.options(options, selected = 1) }



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/nitro/builders/xhtml.rb', line 57

def options(opts, selected = nil)
	if opts.is_a?(Array)
		selected = selected.to_i
		opts.each_with_index do |label, value|
			if value == selected
				self << %|<option value="#{value}" selected="1">#{label}</option>|
			else
				self << %|<option value="#{value}">#{label}</option>|
			end
		end
	else
		opts.each do |label, value|
			if value == selected
				self << %|<option value="#{value}" selected="1">#{label}</option>|
			else
				self << %|<option value="#{value}">#{label}</option>|
			end
		end
	end
end

#select(*args) ⇒ Object

Render select.

The first argument can be a String denoting the default option. – gmosx: hmmm String defines select, this is bad! ++



25
26
27
28
29
30
31
# File 'lib/nitro/builders/xhtml.rb', line 25

def select(*args)
	attrs = args.last.is_a?(Hash) ? args.pop : nil
	start_tag('select', attrs)
	self << %|<option>#{args.first}</option>| unless args.empty?
	yield
	end_tag('select')
end