支持CGI程序的库。请参考下文以了解CGI协议之详情。
* http://Web.Golux.Com/coar/cgi/draft-coar-cgi-v11-03.txt
用例
获取form field的值
require "cgi"
cgi = CGI.new
values = cgi['field_name'] # <== 'field_name' 的数组
# 若尚未指定'field_name', 则返回 []。 (1.6)
# 若尚未指定'field_name', 则返回 ""。 (1.8)
fields = cgi.keys # <== field name的数组
# 若form中有名为'field_name'的field name, 则为真
cgi.has_key?('field_name')
cgi.include?('field_name')
以哈希表的方式获取form field的值
以哈希表方式获取form的值时,需要使用CGI#params。
例
require "cgi"
cgi = CGI.new
params = cgi.params
因为CGI#params每次都返回相同的Hash对象,所以还可以这样。
cgi.params['new_field_name'] = ["value"] # 添加新参数
cgi.params['field_name'] = ["new_value"] # 改变参数值
cgi.params.delete('field_name') # 删除参数
cgi.params.clear # 删除所有参数
将form field的值存入文件
最简单的方法就是使用PStore。
# 保存
require 'pstore'
db = PStore.new("query.db")
db.transaction do
db["params"] = cgi.params
end
# 恢复
require 'pstore'
db = PStore.new("query.db")
db.transaction do
cgi.params = db["params"]
end
但是,随着Ruby版本的不断更新, PStore数据的兼容性可能会出现问题, 所以若需要长期保存数据的话, 还请您慎重选择。
请参考PStore。
获取multipart field的值
ruby-1.6 のcgi.rb:
require "cgi"
cgi = CGI.new
values = cgi['field_name'] # field_name的数组
values[0].read # values[0]的内容
values[0].local_path # values[0]的本地文件的路径
values[0].original_filename # values[0]原来的名称
values[0].content_type # values[0]的content_type
values[0] 是 Tempfile对象。
ruby-1.8 的cgi.rb:
require "cgi"
cgi = CGI.new
value = cgi['field_name'] # field_name的数组
value.read # 内容
value.local_path # 本地文件的路径
value.original_filename # 原来的名称
value.content_type # content_type
向客户端传递cookie
require "cgi"
cgi = CGI.new
for name, cookie in cgi.cookies
cookie.expires = Time.now + 30
end
cgi.out("cookie" => cgi.cookies){"string"}
cgi.cookies # { "name1" => cookie1, "name2" => cookie2, ... }
require "cgi"
cgi = CGI.new
cgi.cookies['name'].expires = Time.now + 30
cgi.out("cookie" => cgi.cookies['name']){"string"}
从客户端获取cookie
require "cgi"
cgi = CGI.new
values = cgi.cookies['name'] # values是name cookie的值的数组。
# 若name cookie不存在, 则返回空数组。
names = cgi.cookies.keys # 所有cookie名称的数组
取得与CGI相关的环境变量的值
您可以直接从ENV中取得与CGI相关的环境变量的值, 此外您还可以利用CGI类的方法来获得这些值. 通常情况下,将环境变量名变为小写之后就得到了相应的方法名. 例如AUTH_TYPE就与CGI#auth_type相对应。
require "cgi"
cgi = CGI.new
value = cgi.auth_type
这些环境变量包括
* AUTH_TYPE
* CONTENT_LENGTH
* CONTENT_TYPE
* GATEWAY_INTERFACE
* PATH_INFO
* PATH_TRANSLATED
* QUERY_STRING
* REMOTE_ADDR
* REMOTE_HOST
* REMOTE_IDENT
* REMOTE_USER
* REQUEST_METHOD
* SCRIPT_NAME
* SERVER_NAME
* SERVER_PORT
* SERVER_PROTOCOL
* SERVER_SOFTWARE
其中#content_length和#server_port返回整数, 其他方法返回字符串。
HTTP_COOKIE和HTTP_COOKIE2分别对应于raw_cookie和raw_cookie2。
value = cgi.raw_cookie # ENV["HTTP_COOKIE"]
value = cgi.raw_cookie2 # ENV["HTTP_COOKIE2"]
此外,将下列这些与HTTP相关的环境变量名中的HTTP_部分去掉之后再变为小写,就得到了对应的方法名。
例
value = cgi.accept # ENV["HTTP_ACCEPT"]
value = cgi.accept_charset # ENV["HTTP_ACCEPT_CHARSET"]
这类环境变量包括
* HTTP_ACCEPT
* HTTP_ACCEPT_CHARSET
* HTTP_ACCEPT_ENCODING
* HTTP_ACCEPT_LANGUAGE
* HTTP_CACHE_CONTROL
* HTTP_FROM
* HTTP_HOST
* HTTP_NEGOTIATE
* HTTP_PRAGMA
* HTTP_REFERER
* HTTP_USER_AGENT
请参考<URL:
http://www.w3.org/CGI/>来了解与CGI相关的环境变量的详情。
将HTTP Header和HTML输出到标准输出
require "cgi"
cgi = CGI.new("html3") # 添加生成HTML的方法
cgi.out() do
cgi.html() do
cgi.head{ cgi.title{"TITLE"} } +
cgi.body() do
cgi.form() do
cgi.textarea("get_text") +
cgi.br +
cgi.submit
end +
cgi.pre() do
CGI::escapeHTML(
"params: " + cgi.params.inspect + "\n" +
"cookies: " + cgi.cookies.inspect + "\n" +
ENV.collect() do |key, value|
key + " --> " + value + "\n"
end.join("")
)
end
end
end
end
# 添加生成HTML的方法
CGI.new("html3") # html3.2
CGI.new("html4") # html4.0 (Strict)
CGI.new("html4Tr") # html4.0 Transitional
CGI.new("html4Fr") # html4.0 Frameset
上传文件
请参考[ruby-list:25399]。
class CGI
类方法
CGI.escape(string)
将string进行URL编码之后返回结果。
p CGI::escape('%##') #=> "%40%23%23"
CGI.unescape(string)
将string进行URL解码之后返回结果。
p CGI::unescape('%40%23%23') #=> "@##"
CGI.escapeHTML(string)
对string中的 &"<> 进行编码并返回结果。
p CGI::escapeHTML("3 > 1") #=> "3 > 1"
CGI.unescapeHTML(string)
只对string中的 & > < " 和带有数值的元素(�ffff 等)进行解码。
p CGI::unescapeHTML("3 > 1") #=> "3 > 1"
CGI.escapeElement(string, *elements)
只对elements所指元素的标签(tag)进行编码。
例:
p CGI::escapeElement('<BR><A HREF="url"></A>', "A", "IMG")
# => "<BR><A HREF="url"></A>"
p CGI::escapeElement('<BR><A HREF="url"></A>', ["A", "IMG"])
# => "<BR><A HREF="url"></A>"
CGI.unescapeElement(string, *element)
只对特定元素进行HTML解码。
例:
print CGI::unescapeElement('<BR><A HREF="url"></A>', "A", "IMG")
# => "<BR><A HREF="url"></A>"
print CGI::unescapeElement('<BR><A HREF="url"></A>', %w(A IMG))
# => "<BR><A HREF="url"></A>"
CGI.rfc1123_date(time)
将time时间变为RFC 1123格式的字符串。
例:
CGI::rfc1123_date(Time.now)
# => Sat, 1 Jan 2000 00:00:00 GMT
CGI.parse(query)
对QUERY_STRING进行解析。
例:
params = CGI::parse("query_string")
# {"name1" => ["value1", "value2", ...],
# "name2" => ["value1", "value2", ...], ... }
CGI.pretty(string, shift = " ")
整理HTML代码,增强其可读性。
例:
print CGI::pretty("<HTML><BODY></BODY></HTML>")
# <HTML>
# <BODY>
# </BODY>
# </HTML>
print CGI::pretty("<HTML><BODY></BODY></HTML>", "\t")
# <HTML>
# <BODY>
# </BODY>
# </HTML>
方法
CGI#header(headers = "text/html")
按照headers的要求输出HTTP头。若不想借助CGI#out,而是要自定义输出HTML时,可以使用该方法。该方法不会对字符串进行编码。
例:
header
# Content-Type: text/html
header("text/plain")
# Content-Type: text/plain
header({"nph" => true,
"status" => "OK", # == "200 OK"
# "status" => "200 GOOD",
"server" => ENV['SERVER_SOFTWARE'],
"connection" => "close",
"type" => "text/html",
"charset" => "iso-2022-jp",
# Content-Type: text/html; charset=iso-2022-jp
"language" => "ja",
"expires" => Time.now + 30,
"cookie" => [cookie1, cookie2],
"my_header1" => "my_value"
"my_header2" => "my_value"})
status参数可以使用下列字符串。
"OK" --> "200 OK"
"PARTIAL_CONTENT" --> "206 Partial Content"
"MULTIPLE_CHOICES" --> "300 Multiple Choices"
"MOVED" --> "301 Moved Permanently"
"REDIRECT" --> "302 Found"
"NOT_MODIFIED" --> "304 Not Modified"
"BAD_REQUEST" --> "400 Bad Request"
"AUTH_REQUIRED" --> "401 Authorization Required"
"FORBIDDEN" --> "403 Forbidden"
"NOT_FOUND" --> "404 Not Found"
"METHOD_NOT_ALLOWED" --> "405 Method Not Allowed"
"NOT_ACCEPTABLE" --> "406 Not Acceptable"
"LENGTH_REQUIRED" --> "411 Length Required"
"PRECONDITION_FAILED" --> "412 Rrecondition Failed"
"SERVER_ERROR" --> "500 Internal Server Error"
"NOT_IMPLEMENTED" --> "501 Method Not Implemented"
"BAD_GATEWAY" --> "502 Bad Gateway"
"VARIANT_ALSO_VARIES" --> "506 Variant Also Negotiates"
例
cgi = CGI.new('html3')
print cgi.header({"charset" => "shift_jis"})
print "<html><head><title>TITLE</title></head>\r\n"
print "<body>BODY</body></html>\r\n"
CGI#out(options = "text/html") { .... }
输出HTTP头和块中给出的字符串。
cgi = CGI.new
cgi.out{ "string" }
# Content-Type: text/html
# Content-Length: 6
#
# string
cgi.out("text/plain"){ "string" }
# Content-Type: text/plain
# Content-Length: 6
#
# string
cgi.out({"nph" => true,
"status" => "OK", # == "200 OK"
"server" => ENV['SERVER_SOFTWARE'],
"connection" => "close",
"type" => "text/html",
"charset" => "iso-2022-jp",
# Content-Type: text/html; charset=iso-2022-jp
"language" => "ja",
"expires" => Time.now + (3600 * 24 * 30),
"cookie" => [cookie1, cookie2],
"my_header1" => "my_value",
"my_header2" => "my_value"}){ "string" }
若遇到HEAD request(REQUEST_METHOD == "HEAD"),则只输出HTTP头。
若charset为"iso-2022-jp"、"euc-jp"或"shift_jis"其中之一时,会自动编码字符串,并将language设为"ja"。
CGI#print(*strings)
将参数的字符串发送到标准输出。
例:
cgi = CGI.new
cgi.print "This line is a part of content body.\r\n"
CGI#auth_type
ENV['AUTH_TYPE']
CGI#content_length
ENV['CONTENT_LENGTH']
CGI#content_type
ENV['CONTENT_TYPE']
CGI#gateway_interface
ENV['GATEWAY_INTERFACE']
CGI#path_info
ENV['PATH_INFO']
CGI#path_translated
ENV['PATH_TRANSLATED']
CGI#query_string
ENV['QUERY_STRING']
CGI#remote_addr
ENV['REMOTE_ADDR']
CGI#remote_host
ENV['REMOTE_HOST']
CGI#remote_ident
ENV['REMOTE_IDENT']
CGI#remote_user
ENV['REMOTE_USER']
CGI#request_method
ENV['REQUEST_METHOD']
CGI#script_name
ENV['SCRIPT_NAME']
CGI#server_name
ENV['SERVER_NAME']
CGI#server_port
ENV['SERVER_PORT']
CGI#server_protocol
ENV['SERVER_PROTOCOL']
CGI#server_software
ENV['SERVER_SOFTWARE']
CGI#raw_cookie
ENV["HTTP_COOKIE"]
CGI#raw_cookie2
ENV["HTTP_COOKIE2"]
CGI#accept
ENV['HTTP_ACCEPT']
CGI#accept_charset
ENV['HTTP_ACCEPT_CHARSET']
CGI#accept_encoding
ENV['HTTP_ACCEPT_ENCODING']
CGI#accept_language
ENV['HTTP_ACCEPT_LANGUAGE']
CGI#cache_control
ENV['HTTP_CACHE_CONTROL']
CGI#from
ENV['HTTP_FROM']
CGI#host
ENV['HTTP_HOST']
CGI#negotiate
ENV['HTTP_NEGOTIATE']
CGI#pragma
ENV['HTTP_PRAGMA']
CGI#referer
ENV['HTTP_REFERER']
CGI#user_agent
ENV['HTTP_USER_AGENT']
class CGI::Cookie
类方法
CGI::Cookie.new(name = "", *value)
生成cookie对象。
例:
cookie1 = CGI::Cookie::new("name", "value1", "value2", ...)
cookie1 = CGI::Cookie::new({"name" => "name", "value" => "value"})
cookie1 = CGI::Cookie::new({'name' => 'name',
'value' => ['value1', 'value2', ...],
'path' => 'path', # optional
'domain' => 'domain', # optional
'expires' => Time.now, # optional
'secure' => true # optional
})
cgi.out({"cookie" => [cookie1, cookie2]}){ "string" }
name = cookie1.name
values = cookie1.value
path = cookie1.path
domain = cookie1.domain
expires = cookie1.expires
secure = cookie1.secure
cookie1.name = 'name'
cookie1.value = ['value1', 'value2', ...]
cookie1.path = 'path'
cookie1.domain = 'domain'
cookie1.expires = Time.now + 30
cookie1.secure = true
CGI::Cookie.parse(raw_cookie)
解析cookie字符串。
例:
cookies = CGI::Cookie::parse("raw_cookie_string")
# { "name1" => cookie1, "name2" => cookie2, ... }
CGI::Cookie#name
CGI::Cookie#name=
CGI::Cookie#value
CGI::Cookie#value=
CGI::Cookie#path
CGI::Cookie#path=
CGI::Cookie#domain
CGI::Cookie#domain=
CGI::Cookie#expires
CGI::Cookie#expires=
CGI::Cookie#secure
CGI::Cookie#secure=
Cookie 对象的属性。
例:
cookie1 = CGI::Cookie::new("name", "value1", "value2", ...)
cookie1 = CGI::Cookie::new({"name" => "name", "value" => "value"})
cookie1 = CGI::Cookie::new({'name' => 'name',
'value' => ['value1', 'value2', ...],
'path' => 'path', # optional
'domain' => 'domain', # optional
'expires' => Time.now, # optional
'secure' => true # optional
})
cgi.out({"cookie" => [cookie1, cookie2]}){ "string" }
name = cookie1.name
values = cookie1.value
path = cookie1.path
domain = cookie1.domain
expires = cookie1.expires
secure = cookie1.secure
cookie1.name = 'name'
cookie1.value = ['value1', 'value2', ...]
cookie1.path = 'path'
cookie1.domain = 'domain'
cookie1.expires = Time.now + 30
cookie1.secure = true
输出HTML元素的方法
例:
cgi = CGI.new("html3") # 添加生成HTML的方法
cgi.element
cgi.element{ "string" }
cgi.element({ "ATTRILUTE1" => "value1", "ATTRIBUTE2" => "value2" })
cgi.element({ "ATTRILUTE1" => "value1", "ATTRIBUTE2" => "value2" }){ "string" }
# 添加生成HTML的方法
CGI.new("html3") # html3.2
CGI.new("html4") # html4.0 (Strict)
CGI.new("html4Tr") # html4.0 Transitional
CGI.new("html4Fr") # html4.0 Frameset
CGI::HtmlExtension#a(href = "")
例:
a("url")
# = a({ "HREF" => "url" })
CGI::HtmlExtension#base(href = "")
例:
base("url")
# = base({ "HREF" => "url" })
CGI::HtmlExtension#blockquote(cite = nil)
例:
blockquote("url"){ "string" }
# = blockquote({ "CITE" => "url" }){ "string" }
CGI::HtmlExtension#caption(align = nil)
例:
caption("align"){ "string" }
# = caption({ "ALIGN" => "align" }){ "string" }
CGI::HtmlExtension#checkbox(name = "", value = nil, checked = nil)
例:
checkbox("name")
# = checkbox({ "NAME" => "name" })
checkbox("name", "value")
# = checkbox({ "NAME" => "name", "VALUE" => "value" })
checkbox("name", "value", true)
# = checkbox({ "NAME" => "name", "VALUE" => "value", "CHECKED" => true })
CGI::HtmlExtension#checkbox_group(name = "", *values)
例:
checkbox_group("name", "foo", "bar", "baz")
# <INPUT TYPE="checkbox" NAME="name" VALUE="foo">foo
# <INPUT TYPE="checkbox" NAME="name" VALUE="bar">bar
# <INPUT TYPE="checkbox" NAME="name" VALUE="baz">baz
checkbox_group("name", ["foo"], ["bar", true], "baz")
# <INPUT TYPE="checkbox" NAME="name" VALUE="foo">foo
# <INPUT TYPE="checkbox" SELECTED NAME="name" VALUE="bar">bar
# <INPUT TYPE="checkbox" NAME="name" VALUE="baz">baz
checkbox_group("name", ["1", "Foo"], ["2", "Bar", true], "Baz")
# <INPUT TYPE="checkbox" NAME="name" VALUE="1">Foo
# <INPUT TYPE="checkbox" SELECTED NAME="name" VALUE="2">Bar
# <INPUT TYPE="checkbox" NAME="name" VALUE="Baz">Baz
checkbox_group({ "NAME" => "name",
"VALUES" => ["foo", "bar", "baz"] })
checkbox_group({ "NAME" => "name",
"VALUES" => [["foo"], ["bar", true], "baz"] })
checkbox_group({ "NAME" => "name",
"VALUES" => [["1", "Foo"], ["2", "Bar", true], "Baz"] })
CGI::HtmlExtension#file_field(name = "", size = 20, maxlength = nil)
例:
file_field("name")
# <INPUT TYPE="file" NAME="name" SIZE="20">
file_field("name", 40)
# <INPUT TYPE="file" NAME="name" SIZE="40">
file_field("name", 40, 100)
# <INPUT TYPE="file" NAME="name" SIZE="40" MAXLENGTH="100">
file_field({ "NAME" => "name", "SIZE" => 40 })
# <INPUT TYPE="file" NAME="name" SIZE="40">
CGI::HtmlExtension#form(method = "post", action = nil, enctype = "application/x-www-form-urlencoded")
例:
form{ "string" }
# <FORM METHOD="post" ENCTYPE="application/x-www-form-urlencoded">string</FORM>
form("get"){ "string" }
# <FORM METHOD="get" ENCTYPE="application/x-www-form-urlencoded">string</FORM>
form("get", "url"){ "string" }
# <FORM METHOD="get" ACTION="url" ENCTYPE="application/x-www-form-urlencoded">string</FORM>
form({"METHOD" => "post", ENCTYPE => "enctype"}){ "string" }
# <FORM METHOD="post" ENCTYPE="enctype">string</FORM>
CGI::HtmlExtension#hidden(name = "", value = nil)
例:
hidden("name")
# <INPUT TYPE="hidden" NAME="name">
hidden("name", "value")
# <INPUT TYPE="hidden" NAME="name" VALUE="value">
hidden({ "NAME" => "name", "VALUE" => "reset", "ID" => "foo" })
# <INPUT TYPE="hidden" NAME="name" VALUE="value" ID="foo">
CGI::HtmlExtension#html(attributes = {})
例:
html{ "string" }
# <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"><HTML>string</HTML>
html({ "LANG" => "ja" }){ "string" }
# <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"><HTML LANG="ja">string</HTML>
html({ "DOCTYPE" => false }){ "string" }
# <HTML>string</HTML>
html({ "DOCTYPE" => '<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">' }){ "string" }
# <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN"><HTML>string</HTML>
html({ "PRETTY" => " " }){ "<BODY></BODY>" }
# <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
# <HTML>
# <BODY>
# </BODY>
# </HTML>
html({ "PRETTY" => "\t" }){ "<BODY></BODY>" }
# <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
# <HTML>
# <BODY>
# </BODY>
# </HTML>
html("PRETTY"){ "<BODY></BODY>" }
# = html({ "PRETTY" => " " }){ "<BODY></BODY>" }
html(if $VERBOSE then "PRETTY" end){ "HTML string" }
CGI::HtmlExtension#image_button(src = "", name = nil, alt = nil)
例:
image_button("url")
# <INPUT TYPE="image" SRC="url">
image_button("url", "name", "string")
# <INPUT TYPE="image" SRC="url" NAME="name" ALT="string">
image_button({ "SRC" => "url", "ATL" => "strng" })
# <INPUT TYPE="image" SRC="url" ALT="string">
CGI::HtmlExtension#img(src = "", alt = "", width = nil, height = nil)
例:
img("src", "alt", 100, 50)
# <IMG SRC="src" ALT="alt" WIDTH="100" HEIGHT="50">
img({ "SRC" => "src", "ALT" => "alt", "WIDTH" => 100, "HEIGHT" => 50 })
# <IMG SRC="src" ALT="alt" WIDTH="100" HEIGHT="50">
CGI::HtmlExtension#multipart_form(action = nil, enctype = "multipart/form-data")
例:
multipart_form{ "string" }
# <FORM METHOD="post" ENCTYPE="multipart/form-data">string</FORM>
multipart_form("url"){ "string" }
# <FORM METHOD="post" ACTION="url" ENCTYPE="multipart/form-data">string</FORM>
CGI::HtmlExtension#password_field(name = "", value = nil, size = 40, maxlength = nil)
例:
password_field("name")
# <INPUT TYPE="password" NAME="name" SIZE="40">
password_field("name", "value")
# <INPUT TYPE="password" NAME="name" VALUE="value" SIZE="40">
password_field("password", "value", 80, 200)
# <INPUT TYPE="password" NAME="name" VALUE="value" SIZE="80" MAXLENGTH="200">
password_field({ "NAME" => "name", "VALUE" => "value" })
# <INPUT TYPE="password" NAME="name" VALUE="value">
CGI::HtmlExtension#popup_menu(name = "", *values)
例:
popup_menu("name", "foo", "bar", "baz")
# <SELECT NAME="name">
# <OPTION VALUE="foo">foo</OPTION>
# <OPTION VALUE="bar">bar</OPTION>
# <OPTION VALUE="baz">baz</OPTION>
# </SELECT>
popup_menu("name", ["foo"], ["bar", true], "baz")
# <SELECT NAME="name">
# <OPTION VALUE="foo">foo</OPTION>
# <OPTION VALUE="bar" SELECTED>bar</OPTION>
# <OPTION VALUE="baz">baz</OPTION>
# </SELECT>
popup_menu("name", ["1", "Foo"], ["2", "Bar", true], "Baz")
# <SELECT NAME="name">
# <OPTION VALUE="1">Foo</OPTION>
# <OPTION SELECTED VALUE="2">Bar</OPTION>
# <OPTION VALUE="Baz">Baz</OPTION>
# </SELECT>
popup_menu({"NAME" => "name", "SIZE" => 2, "MULTIPLE" => true,
"VALUES" => [["1", "Foo"], ["2", "Bar", true], "Baz"] })
# <SELECT NAME="name" MULTIPLE SIZE="2">
# <OPTION VALUE="1">Foo</OPTION>
# <OPTION SELECTED VALUE="2">Bar</OPTION>
# <OPTION VALUE="Baz">Baz</OPTION>
# </SELECT>
CGI::HtmlExtension#radio_button(name = "", value = nil, checked = nil)
例:
radio_button("name", "value")
# <INPUT TYPE="radio" NAME="name" VALUE="value">
radio_button("name", "value", true)
# <INPUT TYPE="radio" NAME="name" VALUE="value" CHECKED>
radio_button({ "NAME" => "name", "VALUE" => "value", "ID" => "foo" })
# <INPUT TYPE="radio" NAME="name" VALUE="value" ID="foo">
CGI::HtmlExtension#radio_group(name = "", *values)
例:
radio_group("name", "foo", "bar", "baz")
# <INPUT TYPE="radio" NAME="name" VALUE="foo">foo
# <INPUT TYPE="radio" NAME="name" VALUE="bar">bar
# <INPUT TYPE="radio" NAME="name" VALUE="baz">baz
radio_group("name", ["foo"], ["bar", true], "baz")
# <INPUT TYPE="radio" NAME="name" VALUE="foo">foo
# <INPUT TYPE="radio" CHECKED NAME="name" VALUE="bar">bar
# <INPUT TYPE="radio" NAME="name" VALUE="baz">baz
radio_group("name", ["1", "Foo"], ["2", "Bar", true], "Baz")
# <INPUT TYPE="radio" NAME="name" VALUE="1">Foo
# <INPUT TYPE="radio" CHECKED NAME="name" VALUE="2">Bar
# <INPUT TYPE="radio" NAME="name" VALUE="Baz">Baz
radio_group({ "NAME" => "name",
"VALUES" => ["foo", "bar", "baz"] })
radio_group({ "NAME" => "name",
"VALUES" => [["foo"], ["bar", true], "baz"] })
radio_group({ "NAME" => "name",
"VALUES" => [["1", "Foo"], ["2", "Bar", true], "Baz"] })
CGI::HtmlExtension#reset(value = nil, name = nil)
例:
reset
# <INPUT TYPE="reset">
reset("reset")
# <INPUT TYPE="reset" VALUE="reset">
reset({ "VALUE" => "reset", "ID" => "foo" })
# <INPUT TYPE="reset" VALUE="reset" ID="foo">
CGI::HtmlExtension#scrolling_list(name = "", *values)
例:
scrolling_list({"NAME" => "name", "SIZE" => 2, "MULTIPLE" => true,
"VALUES" => [["1", "Foo"], ["2", "Bar", true], "Baz"] })
# <SELECT NAME="name" MULTIPLE SIZE="2">
# <OPTION VALUE="1">Foo</OPTION>
# <OPTION SELECTED VALUE="2">Bar</OPTION>
# <OPTION VALUE="Baz">Baz</OPTION>
# </SELECT>
CGI::HtmlExtension#submit(value = nil, name = nil)
例:
submit
# <INPUT TYPE="submit">
submit("ok")
# <INPUT TYPE="submit" VALUE="ok">
submit("ok", "button1")
# <INPUT TYPE="submit" VALUE="ok" NAME="button1">
submit({ "VALUE" => "ok", "NAME" => "button1", "ID" => "foo" })
# <INPUT TYPE="submit" VALUE="ok" NAME="button1" ID="foo">
CGI::HtmlExtension#text_field(name = "", value = nil, size = 40, maxlength = nil)
例:
text_field("name")
# <INPUT TYPE="text" NAME="name" SIZE="40">
text_field("name", "value")
# <INPUT TYPE="text" NAME="name" VALUE="value" SIZE="40">
text_field("name", "value", 80)
# <INPUT TYPE="text" NAME="name" VALUE="value" SIZE="80">
text_field("name", "value", 80, 200)
# <INPUT TYPE="text" NAME="name" VALUE="value" SIZE="80" MAXLENGTH="200">
text_field({ "NAME" => "name", "VALUE" => "value" })
# <INPUT TYPE="text" NAME="name" VALUE="value">
CGI::HtmlExtension#textarea(name = "", cols = 70, rows = 10)
例:
textarea("name")
# = textarea({ "NAME" => "name", "COLS" => 70, "ROWS" => 10 })
textarea("name", 40, 5)
# = textarea({ "NAME" => "name", "COLS" => 40, "ROWS" => 5 })