Ruby 2.4

CGI::HtmlExtension

CGI :: HtmlExtension模块

Mixin模块提供HTML生成方法。

例如,

cgi.a("http://www.example.com") { "Example" } # => "<A HREF=\"http://www.example.com\">Example</A>"

模块HTML3,HTML4等,含有较多的基本的HTML代方法(#title#h1等)。

有关详细示例,请参见CGI课程。

公共实例方法

a(href = "") { || ... } Show source

生成一个Anchor元素作为一个字符串。

href 可以是字符串,给出HREF属性的URL,也可以是元素属性的散列。

元素的主体是由传入的无参数块返回的字符串。

a("http://www.example.com") { "Example" } # => "<A HREF=\"http://www.example.com\">Example</A>" a("HREF" => "http://www.example.com", "TARGET" => "_top") { "Example" } # => "<A HREF=\"http://www.example.com\" TARGET=\"_top\">Example</A>"

调用超类方法

# File lib/cgi/html.rb, line 96 def a(href = "") # :yield: attributes = if href.kind_of?(String) { "HREF" => href } else href end super(attributes) end

base(href = "") { || ... } Show source

生成一个文档基URI元素作为一个字符串。

href 可以通过字符串给出HREF属性的基本URL,也可以是元素属性的has。

传入的无参数块将被忽略。

base("http://www.example.com/cgi") # => "<BASE HREF=\"http://www.example.com/cgi\">"

调用超类方法

# File lib/cgi/html.rb, line 114 def base(href = "") # :yield: attributes = if href.kind_of?(String) { "HREF" => href } else href end super(attributes) end

blockquote(cite = {}) { || ... } Show source

生成一个BlockQuote元素作为一个字符串。

cite 既可以是字符串,也可以是引用文本源的URI,也可以是散列,给出元素的所有属性,或者可以省略,在这种情况下元素没有属性。

正文由传入的无参数块提供

blockquote("http://www.example.com/quotes/foo.html") { "Foo!" } #=> "<BLOCKQUOTE CITE=\"http://www.example.com/quotes/foo.html\">Foo!</BLOCKQUOTE>

调用超类方法

# File lib/cgi/html.rb, line 133 def blockquote(cite = {}) # :yield: attributes = if cite.kind_of?(String) { "CITE" => cite } else cite end super(attributes) end

caption(align = {}) { || ... } Show source

以字符串形式生成一个Table Caption元素。

align可以是字符串,给出标题的对齐(顶部,底部,左侧或右侧之一)。它可以是元素所有属性的散列。或者它可以省略。

元素的主体由传入的无参数块提供。

caption("left") { "Capital Cities" } # => <CAPTION ALIGN=\"left\">Capital Cities</CAPTION>

调用超类方法

# File lib/cgi/html.rb, line 153 def caption(align = {}) # :yield: attributes = if align.kind_of?(String) { "ALIGN" => align } else align end super(attributes) end

checkbox(name = "", value = nil, checked = nil) Show source

生成复选框输入元素作为字符串。

所述元素的属性可被指定为三个参数,namevalue,和checkedchecked是一个布尔值; 如果为true,CHECKED属性将包含在元素中。

或者,可以将属性指定为散列。

checkbox("name") # = checkbox("NAME" => "name") checkbox("name", "value") # = checkbox("NAME" => "name", "VALUE" => "value") checkbox("name", "value", true) # = checkbox("NAME" => "name", "VALUE" => "value", "CHECKED" => true)

# File lib/cgi/html.rb, line 179 def checkbox(name = "", value = nil, checked = nil) attributes = if name.kind_of?(String) { "TYPE" => "checkbox", "NAME" => name, "VALUE" => value, "CHECKED" => checked } else name["TYPE"] = "checkbox" name end input(attributes) end

checkbox_group(name = "", *values) Show source

以String形式生成一系列复选框元素。

复选框将具有相同的name属性。每个复选框后面都有一个标签。每个值都会有一个复选框。每个值都可以指定为一个字符串,该字符串既可以用作VALUE属性的值,也可以用作该复选框的标签。单元素数组具有相同的效果。

每个值也可以被指定为三元素数组。第一个元素是VALUE属性; 第二个是标签; 第三个是指定这个复选框是否被CHECKED的布尔值。

通过省略value元素(默认为与标签相同)或布尔型checked元素(默认为false),也可以将每个值指定为双元素数组。

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" CHECKED 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" CHECKED 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"])

# File lib/cgi/html.rb, line 233 def checkbox_group(name = "", *values) if name.kind_of?(Hash) values = name["VALUES"] name = name["NAME"] end values.collect{|value| if value.kind_of?(String) checkbox(name, value) + value else if value[-1] == true || value[-1] == false checkbox(name, value[0], value[-1]) + value[-2] else checkbox(name, value[0]) + value[-1] end end }.join end

file_field(name = "", size = 20, maxlength = nil) Show source

生成一个文件上传输入元素作为字符串。

所述元素的属性可被指定为三个参数,namesize,和maxlengthmaxlength是文件名称的最大长度,而不是文件内容的最大长度。

或者,可以将属性指定为散列。

有关包含文件上载的表单,请参阅multipart_form()。

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">

# File lib/cgi/html.rb, line 275 def file_field(name = "", size = 20, maxlength = nil) attributes = if name.kind_of?(String) { "TYPE" => "file", "NAME" => name, "SIZE" => size.to_s } else name["TYPE"] = "file" name end attributes["MAXLENGTH"] = maxlength.to_s if maxlength input(attributes) end

form(method = "post", action = script_name, enctype = "application/x-www-form-urlencoded") { || ... } Show source

生成一个Form元素作为一个字符串。

method应该是“get”或“post”,并且默认为后者。action默认为当前的CGI脚本名称。enctype默认为“application / x-www-form-urlencoded”。

或者,可以将属性指定为散列。

对于包含文件上传的表单,另请参阅multipart_form()。

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>

调用超类方法

# File lib/cgi/html.rb, line 309 def form(method = "post", action = script_name, enctype = "application/x-www-form-urlencoded") attributes = if method.kind_of?(String) { "METHOD" => method, "ACTION" => action, "ENCTYPE" => enctype } else unless method.has_key?("METHOD") method["METHOD"] = "post" end unless method.has_key?("ENCTYPE") method["ENCTYPE"] = enctype end method end if block_given? body = yield else body = "" end if @output_hidden body << @output_hidden.collect{|k,v| "<INPUT TYPE=\"HIDDEN\" NAME=\"#{k}\" VALUE=\"#{v}\">" }.join end super(attributes){body} end

hidden(name = "", value = nil) Show source

生成一个隐藏的输入元素作为一个字符串。

元素的属性可以被指定为两个参数,name并且value

或者,可以将属性指定为散列。

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">

# File lib/cgi/html.rb, line 350 def hidden(name = "", value = nil) attributes = if name.kind_of?(String) { "TYPE" => "hidden", "NAME" => name, "VALUE" => value } else name["TYPE"] = "hidden" name end input(attributes) end

html(attributes = {}) { || ... } Show source

以字符串形式生成顶级HTML元素。

元素的属性被指定为散列。伪属性“PRETTY”可以用来指定生成的HTML字符串应该缩进。“PRETTY”也可以指定为字符串作为此方法的唯一参数。如果给出,伪属性“DOCTYPE”被用作主要的DOCTYPE SGML标签; 它应该包括这个标签的全部文本,包括尖括号。

html元素的主体以块的形式提供。

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" }

调用超类方法

# File lib/cgi/html.rb, line 402 def html(attributes = {}) # :yield: if nil == attributes attributes = {} elsif "PRETTY" == attributes attributes = { "PRETTY" => true } end pretty = attributes.delete("PRETTY") pretty = " " if true == pretty buf = "" if attributes.has_key?("DOCTYPE") if attributes["DOCTYPE"] buf << attributes.delete("DOCTYPE") else attributes.delete("DOCTYPE") end else buf << doctype end buf << super(attributes) if pretty CGI::pretty(buf, pretty) else buf end end

image_button(src = "", name = nil, alt = nil) Show source

生成一个图像按钮输入元素作为一个字符串。

src是用于按钮的图片的网址。name是输入名称。alt是图像的替代文字。

或者,可以将属性指定为散列。

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", "ALT" => "string") # <INPUT TYPE="image" SRC="url" ALT="string">

# File lib/cgi/html.rb, line 447 def image_button(src = "", name = nil, alt = nil) attributes = if src.kind_of?(String) { "TYPE" => "image", "SRC" => src, "NAME" => name, "ALT" => alt } else src["TYPE"] = "image" src["SRC"] ||= "" src end input(attributes) end

img(src = "", alt = "", width = nil, height = nil) Show source

生成一个图像元素作为一个字符串。

src是图片的网址。alt是图像的替代文字。width是图像的宽度,height是它的高度。

或者,可以将属性指定为散列。

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">

调用超类方法

# File lib/cgi/html.rb, line 473 def img(src = "", alt = "", width = nil, height = nil) attributes = if src.kind_of?(String) { "SRC" => src, "ALT" => alt } else src end attributes["WIDTH"] = width.to_s if width attributes["HEIGHT"] = height.to_s if height super(attributes) end

multipart_form(action = nil, enctype = "multipart/form-data") { || ... } Show source

使用多部分编码生成一个Form元素作为String。

多部分编码用于包含文件上传的表单。

action是要执行的动作。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>

# File lib/cgi/html.rb, line 499 def multipart_form(action = nil, enctype = "multipart/form-data") attributes = if action == nil { "METHOD" => "post", "ENCTYPE" => enctype } elsif action.kind_of?(String) { "METHOD" => "post", "ACTION" => action, "ENCTYPE" => enctype } else unless action.has_key?("METHOD") action["METHOD"] = "post" end unless action.has_key?("ENCTYPE") action["ENCTYPE"] = enctype end action end if block_given? form(attributes){ yield } else form(attributes) end end

password_field(name = "", value = nil, size = 40, maxlength = nil) Show source

生成一个密码输入元素作为一个字符串。

name是输入字段的名称。value是它的默认值。size是输入字段显示的大小。maxlength是输入的密码的最大长度。

或者,可以将属性指定为散列。

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">

# File lib/cgi/html.rb, line 541 def password_field(name = "", value = nil, size = 40, maxlength = nil) attributes = if name.kind_of?(String) { "TYPE" => "password", "NAME" => name, "VALUE" => value, "SIZE" => size.to_s } else name["TYPE"] = "password" name end attributes["MAXLENGTH"] = maxlength.to_s if maxlength input(attributes) end

popup_menu(name = "", *values) Show source

生成一个Select元素作为字符串。

name是元素的名称。 这些值是可以从选择菜单中选择的选项。 每个值可以是一个字符串或一个,两个或三个元素的数组。 如果是字符串或一个元素的数组,则这是该选项的值以及为其显示的文本。 如果是三元素数组,则元素是选项值,显示文本和布尔值,指定此选项是否以选定的方式启动。 双元素版本省略了选项值(默认与显示文本相同)或布尔选择的说明符(默认为false)。

属性和选项也可以指定为散列。在这种情况下,选项被指定为如上所述的值的数组,其中散列键为“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>

# File lib/cgi/html.rb, line 596 def popup_menu(name = "", *values) if name.kind_of?(Hash) values = name["VALUES"] size = name["SIZE"].to_s if name["SIZE"] multiple = name["MULTIPLE"] name = name["NAME"] else size = nil multiple = nil end select{ "NAME" => name, "SIZE" => size, "MULTIPLE" => multiple }){ values.collect{|value| if value.kind_of?(String) option{ "VALUE" => value }){ value } else if value[value.size - 1] == true option{ "VALUE" => value[0], "SELECTED" => true }){ value[value.size - 2] } else option{ "VALUE" => value[0] }){ value[value.size - 1] } end end }.join } end

另外别名为:scrolling_list

radio_button(name = "", value = nil, checked = nil) Show source

生成一个单选按钮输入元素。

name是输入字段的名称。 值是该字段的值(如果选中)。 选中指定是否开始检查字段。

或者,可以将属性指定为散列。

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">

# File lib/cgi/html.rb, line 645 def radio_button(name = "", value = nil, checked = nil) attributes = if name.kind_of?(String) { "TYPE" => "radio", "NAME" => name, "VALUE" => value, "CHECKED" => checked } else name["TYPE"] = "radio" name end input(attributes) end

radio_group(name = "", *values) Show source

生成一个单选按钮Input元素的序列,作为一个String。

这与checkbox_group()相同。但是,检查组中有多个单选按钮是无效的。

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"])

# File lib/cgi/html.rb, line 684 def radio_group(name = "", *values) if name.kind_of?(Hash) values = name["VALUES"] name = name["NAME"] end values.collect{|value| if value.kind_of?(String) radio_button(name, value) + value else if value[-1] == true || value[-1] == false radio_button(name, value[0], value[-1]) + value[-2] else radio_button(name, value[0]) + value[-1] end end }.join end

reset(value = nil, name = nil) Show source

生成一个重置按钮输入元素,作为一个字符串。

这将表单上的值重置为其初始值。value是按钮上显示的文字。name是这个按钮的名字。

或者,可以将属性指定为散列。

reset # <INPUT TYPE="reset"> reset("reset") # <INPUT TYPE="reset" VALUE="reset"> reset("VALUE" => "reset", "ID" => "foo") # <INPUT TYPE="reset" VALUE="reset" ID="foo">

# File lib/cgi/html.rb, line 719 def reset(value = nil, name = nil) attributes = if (not value) or value.kind_of?(String) { "TYPE" => "reset", "VALUE" => value, "NAME" => name } else value["TYPE"] = "reset" value end input(attributes) end

scrolling_list(name = "", *values)

Alias for: popup_menu

submit(value = nil, name = nil) Show source

生成一个提交按钮输入元素,作为一个字符串。

value是要在按钮上显示的文字。name是输入的名称。

或者,可以将属性指定为散列。

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">

# File lib/cgi/html.rb, line 749 def submit(value = nil, name = nil) attributes = if (not value) or value.kind_of?(String) { "TYPE" => "submit", "VALUE" => value, "NAME" => name } else value["TYPE"] = "submit" value end input(attributes) end

text_field(name = "", value = nil, size = 40, maxlength = nil) Show source

生成一个文本字段输入元素,作为一个字符串。

name是输入字段的名称。value是它的初始价值。size是输入区域的大小。maxlength是接受的输入的最大长度。

或者,可以将属性指定为散列。

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">

# File lib/cgi/html.rb, line 781 def text_field(name = "", value = nil, size = 40, maxlength = nil) attributes = if name.kind_of?(String) { "TYPE" => "text", "NAME" => name, "VALUE" => value, "SIZE" => size.to_s } else name["TYPE"] = "text" name end attributes["MAXLENGTH"] = maxlength.to_s if maxlength input(attributes) end

textarea(name = "", cols = 70, rows = 10) { || ... } Show source

生成一个TextArea元素,作为一个String。

name是textarea的名字。cols是列数并且rows是显示中的行数。

或者,可以将属性指定为散列。

正文由传入的无参数块提供

textarea("name") # = textarea("NAME" => "name", "COLS" => 70, "ROWS" => 10) textarea("name", 40, 5) # = textarea("NAME" => "name", "COLS" => 40, "ROWS" => 5)

调用超类方法

# File lib/cgi/html.rb, line 807 def textarea(name = "", cols = 70, rows = 10) # :yield: attributes = if name.kind_of?(String) { "NAME" => name, "COLS" => cols.to_s, "ROWS" => rows.to_s } else name end super(attributes) end