Module: CGI::HtmlExtension
- Defined in:
- lib/rubysl/cgi/cgi.rb
Overview
Mixin module providing HTML generation methods.
For example,
cgi.a("http://www.example.com") { "Example" }
# => "<A HREF=\"http://www.example.com\">Example</A>"
Modules Http3, Http4, etc., contain more basic HTML-generation methods (:title, :center, etc.).
See class CGI for a detailed example.
Instance Method Summary collapse
-
#a(href = "") ⇒ Object
Generate an Anchor element as a string.
-
#base(href = "") ⇒ Object
Generate a Document Base URI element as a String.
-
#blockquote(cite = nil) ⇒ Object
Generate a BlockQuote element as a string.
-
#caption(align = nil) ⇒ Object
Generate a Table Caption element as a string.
-
#checkbox(name = "", value = nil, checked = nil) ⇒ Object
Generate a Checkbox Input element as a string.
-
#checkbox_group(name = "", *values) ⇒ Object
Generate a sequence of checkbox elements, as a String.
-
#file_field(name = "", size = 20, maxlength = nil) ⇒ Object
Generate an File Upload Input element as a string.
-
#form(method = "post", action = script_name, enctype = "application/x-www-form-urlencoded") ⇒ Object
Generate a Form element as a string.
-
#hidden(name = "", value = nil) ⇒ Object
Generate a Hidden Input element as a string.
-
#html(attributes = {}) ⇒ Object
Generate a top-level HTML element as a string.
-
#image_button(src = "", name = nil, alt = nil) ⇒ Object
Generate an Image Button Input element as a string.
-
#img(src = "", alt = "", width = nil, height = nil) ⇒ Object
Generate an Image element as a string.
-
#multipart_form(action = nil, enctype = "multipart/form-data") ⇒ Object
Generate a Form element with multipart encoding as a String.
-
#password_field(name = "", value = nil, size = 40, maxlength = nil) ⇒ Object
Generate a Password Input element as a string.
-
#popup_menu(name = "", *values) ⇒ Object
(also: #scrolling_list)
Generate a Select element as a string.
-
#radio_button(name = "", value = nil, checked = nil) ⇒ Object
Generates a radio-button Input element.
-
#radio_group(name = "", *values) ⇒ Object
Generate a sequence of radio button Input elements, as a String.
-
#reset(value = nil, name = nil) ⇒ Object
Generate a reset button Input element, as a String.
-
#submit(value = nil, name = nil) ⇒ Object
Generate a submit button Input element, as a String.
-
#text_field(name = "", value = nil, size = 40, maxlength = nil) ⇒ Object
Generate a text field Input element, as a String.
-
#textarea(name = "", cols = 70, rows = 10) ⇒ Object
Generate a TextArea element, as a String.
Instance Method Details
#a(href = "") ⇒ Object
Generate an Anchor element as a string.
href can either be a string, giving the URL for the HREF attribute, or it can be a hash of the element’s attributes.
The body of the element is the string returned by the no-argument block passed in.
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>"
1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 |
# File 'lib/rubysl/cgi/cgi.rb', line 1329 def a(href = "") # :yield: attributes = if href.kind_of?(String) { "HREF" => href } else href end if block_given? super(attributes){ yield } else super(attributes) end end |
#base(href = "") ⇒ Object
Generate a Document Base URI element as a String.
href can either by a string, giving the base URL for the HREF attribute, or it can be a has of the element’s attributes.
The passed-in no-argument block is ignored.
base("http://www.example.com/cgi")
# => "<BASE HREF=\"http://www.example.com/cgi\">"
1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 |
# File 'lib/rubysl/cgi/cgi.rb', line 1351 def base(href = "") # :yield: attributes = if href.kind_of?(String) { "HREF" => href } else href end if block_given? super(attributes){ yield } else super(attributes) end end |
#blockquote(cite = nil) ⇒ Object
Generate a BlockQuote element as a string.
cite can either be a string, give the URI for the source of the quoted text, or a hash, giving all attributes of the element, or it can be omitted, in which case the element has no attributes.
The body is provided by the passed-in no-argument block
blockquote("http://www.example.com/quotes/foo.html") { "Foo!" }
#=> "<BLOCKQUOTE CITE=\"http://www.example.com/quotes/foo.html\">Foo!</BLOCKQUOTE>
1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 |
# File 'lib/rubysl/cgi/cgi.rb', line 1374 def blockquote(cite = nil) # :yield: attributes = if cite.kind_of?(String) { "CITE" => cite } else cite or "" end if block_given? super(attributes){ yield } else super(attributes) end end |
#caption(align = nil) ⇒ Object
Generate a Table Caption element as a string.
align can be a string, giving the alignment of the caption (one of top, bottom, left, or right). It can be a hash of all the attributes of the element. Or it can be omitted.
The body of the element is provided by the passed-in no-argument block.
caption("left") { "Capital Cities" }
# => <CAPTION ALIGN=\"left\">Capital Cities</CAPTION>
1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 |
# File 'lib/rubysl/cgi/cgi.rb', line 1398 def caption(align = nil) # :yield: attributes = if align.kind_of?(String) { "ALIGN" => align } else align or "" end if block_given? super(attributes){ yield } else super(attributes) end end |
#checkbox(name = "", value = nil, checked = nil) ⇒ Object
Generate a Checkbox Input element as a string.
The attributes of the element can be specified as three arguments, name, value, and checked. checked is a boolean value; if true, the CHECKED attribute will be included in the element.
Alternatively, the attributes can be specified as a hash.
checkbox("name")
# = checkbox("NAME" => "name")
checkbox("name", "value")
# = checkbox("NAME" => "name", "VALUE" => "value")
checkbox("name", "value", true)
# = checkbox("NAME" => "name", "VALUE" => "value", "CHECKED" => true)
1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 |
# File 'lib/rubysl/cgi/cgi.rb', line 1428 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) ⇒ Object
Generate a sequence of checkbox elements, as a String.
The checkboxes will all have the same name attribute. Each checkbox is followed by a label. There will be one checkbox for each value. Each value can be specified as a String, which will be used both as the value of the VALUE attribute and as the label for that checkbox. A single-element array has the same effect.
Each value can also be specified as a three-element array. The first element is the VALUE attribute; the second is the label; and the third is a boolean specifying whether this checkbox is CHECKED.
Each value can also be specified as a two-element array, by omitting either the value element (defaults to the same as the label), or the boolean checked element (defaults to 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"])
1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 |
# File 'lib/rubysl/cgi/cgi.rb', line 1482 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[value.size - 1] == true checkbox(name, value[0], true) + value[value.size - 2] elsif value[value.size - 1] == false checkbox(name, value[0], false) + value[value.size - 2] else checkbox(name, value[0]) + value[value.size - 1] end end }.to_s end |
#file_field(name = "", size = 20, maxlength = nil) ⇒ Object
Generate an File Upload Input element as a string.
The attributes of the element can be specified as three arguments, name, size, and maxlength. maxlength is the maximum length of the file’s name, not of the file’s contents.
Alternatively, the attributes can be specified as a hash.
See #multipart_form() for forms that include file uploads.
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">
1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 |
# File 'lib/rubysl/cgi/cgi.rb', line 1527 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["SIZE"] = name["SIZE"].to_s if name["SIZE"] name["MAXLENGTH"] = name["MAXLENGTH"].to_s if name["MAXLENGTH"] name end attributes["MAXLENGTH"] = maxlength.to_s if maxlength input(attributes) end |
#form(method = "post", action = script_name, enctype = "application/x-www-form-urlencoded") ⇒ Object
Generate a Form element as a string.
method should be either “get” or “post”, and defaults to the latter. action defaults to the current CGI script name. enctype defaults to “application/x-www-form-urlencoded”.
Alternatively, the attributes can be specified as a hash.
See also #multipart_form() for forms that include file uploads.
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>
1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 |
# File 'lib/rubysl/cgi/cgi.rb', line 1563 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}\">" }.to_s end super(attributes){body} end |
#hidden(name = "", value = nil) ⇒ Object
Generate a Hidden Input element as a string.
The attributes of the element can be specified as two arguments, name and value.
Alternatively, the attributes can be specified as a hash.
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">
1604 1605 1606 1607 1608 1609 1610 1611 1612 |
# File 'lib/rubysl/cgi/cgi.rb', line 1604 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 = {}) ⇒ Object
Generate a top-level HTML element as a string.
The attributes of the element are specified as a hash. The pseudo-attribute “PRETTY” can be used to specify that the generated HTML string should be indented. “PRETTY” can also be specified as a string as the sole argument to this method. The pseudo-attribute “DOCTYPE”, if given, is used as the leading DOCTYPE SGML tag; it should include the entire text of this tag, including angle brackets.
The body of the html element is supplied as a block.
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" }
1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 |
# File 'lib/rubysl/cgi/cgi.rb', line 1656 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 if block_given? buf += super(attributes){ yield } else buf += super(attributes) end if pretty CGI::pretty(buf, pretty) else buf end end |
#image_button(src = "", name = nil, alt = nil) ⇒ Object
Generate an Image Button Input element as a string.
src is the URL of the image to use for the button. name is the input name. alt is the alternative text for the image.
Alternatively, the attributes can be specified as a hash.
("url")
# <INPUT TYPE="image" SRC="url">
("url", "name", "string")
# <INPUT TYPE="image" SRC="url" NAME="name" ALT="string">
("SRC" => "url", "ATL" => "strng")
# <INPUT TYPE="image" SRC="url" ALT="string">
1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 |
# File 'lib/rubysl/cgi/cgi.rb', line 1705 def (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) ⇒ Object
Generate an Image element as a string.
src is the URL of the image. alt is the alternative text for the image. width is the width of the image, and height is its height.
Alternatively, the attributes can be specified as a hash.
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">
1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 |
# File 'lib/rubysl/cgi/cgi.rb', line 1731 def img(src = "", alt = "", width = nil, height = nil) attributes = if src.kind_of?(String) { "SRC" => src, "ALT" => alt } else src["WIDTH"] = src["WIDTH"].to_s if src["WIDTH"] src["HEIGHT"] = src["HEIGHT"].to_s if src["HEIGHT"] 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") ⇒ Object
Generate a Form element with multipart encoding as a String.
Multipart encoding is used for forms that include file uploads.
action is the action to perform. enctype is the encoding type, which defaults to “multipart/form-data”.
Alternatively, the attributes can be specified as a hash.
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>
1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 |
# File 'lib/rubysl/cgi/cgi.rb', line 1759 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) ⇒ Object
Generate a Password Input element as a string.
name is the name of the input field. value is its default value. size is the size of the input field display. maxlength is the maximum length of the inputted password.
Alternatively, attributes can be specified as a hash.
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">
1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 |
# File 'lib/rubysl/cgi/cgi.rb', line 1801 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) ⇒ Object Also known as: scrolling_list
Generate a Select element as a string.
name is the name of the element. The values are the options that can be selected from the Select menu. Each value can be a String or a one, two, or three-element Array. If a String or a one-element Array, this is both the value of that option and the text displayed for it. If a three-element Array, the elements are the option value, displayed text, and a boolean value specifying whether this option starts as selected. The two-element version omits either the option value (defaults to the same as the display text) or the boolean selected specifier (defaults to false).
The attributes and options can also be specified as a hash. In this case, options are specified as an array of values as described above, with the hash key of “VALUES”.
("name", "foo", "bar", "baz")
# <SELECT NAME="name">
# <OPTION VALUE="foo">foo</OPTION>
# <OPTION VALUE="bar">bar</OPTION>
# <OPTION VALUE="baz">baz</OPTION>
# </SELECT>
("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>
("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>
("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>
1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 |
# File 'lib/rubysl/cgi/cgi.rb', line 1856 def (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 }.to_s } end |
#radio_button(name = "", value = nil, checked = nil) ⇒ Object
Generates a radio-button Input element.
name is the name of the input field. value is the value of the field if checked. checked specifies whether the field starts off checked.
Alternatively, the attributes can be specified as a hash.
("name", "value")
# <INPUT TYPE="radio" NAME="name" VALUE="value">
("name", "value", true)
# <INPUT TYPE="radio" NAME="name" VALUE="value" CHECKED>
("NAME" => "name", "VALUE" => "value", "ID" => "foo")
# <INPUT TYPE="radio" NAME="name" VALUE="value" ID="foo">
1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 |
# File 'lib/rubysl/cgi/cgi.rb', line 1905 def (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) ⇒ Object
Generate a sequence of radio button Input elements, as a String.
This works the same as #checkbox_group(). However, it is not valid to have more than one radiobutton in a group checked.
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"])
1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 |
# File 'lib/rubysl/cgi/cgi.rb', line 1944 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) (name, value) + value else if value[value.size - 1] == true (name, value[0], true) + value[value.size - 2] elsif value[value.size - 1] == false (name, value[0], false) + value[value.size - 2] else (name, value[0]) + value[value.size - 1] end end }.to_s end |
#reset(value = nil, name = nil) ⇒ Object
Generate a reset button Input element, as a String.
This resets the values on a form to their initial values. value is the text displayed on the button. name is the name of this button.
Alternatively, the attributes can be specified as a hash.
reset
# <INPUT TYPE="reset">
reset("reset")
# <INPUT TYPE="reset" VALUE="reset">
reset("VALUE" => "reset", "ID" => "foo")
# <INPUT TYPE="reset" VALUE="reset" ID="foo">
1982 1983 1984 1985 1986 1987 1988 1989 1990 |
# File 'lib/rubysl/cgi/cgi.rb', line 1982 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 |
#submit(value = nil, name = nil) ⇒ Object
Generate a submit button Input element, as a String.
value is the text to display on the button. name is the name of the input.
Alternatively, the attributes can be specified as a hash.
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">
2012 2013 2014 2015 2016 2017 2018 2019 2020 |
# File 'lib/rubysl/cgi/cgi.rb', line 2012 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) ⇒ Object
Generate a text field Input element, as a String.
name is the name of the input field. value is its initial value. size is the size of the input area. maxlength is the maximum length of input accepted.
Alternatively, the attributes can be specified as a hash.
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">
2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 |
# File 'lib/rubysl/cgi/cgi.rb', line 2044 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) ⇒ Object
Generate a TextArea element, as a String.
name is the name of the textarea. cols is the number of columns and rows is the number of rows in the display.
Alternatively, the attributes can be specified as a hash.
The body is provided by the passed-in no-argument block
textarea("name")
# = textarea("NAME" => "name", "COLS" => 70, "ROWS" => 10)
textarea("name", 40, 5)
# = textarea("NAME" => "name", "COLS" => 40, "ROWS" => 5)
2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 |
# File 'lib/rubysl/cgi/cgi.rb', line 2070 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 if block_given? super(attributes){ yield } else super(attributes) end end |