Module CGI::HtmlExtension
In: lib/cgi.rb

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.

Methods

Public Instance methods

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

[Source]

      # File lib/cgi.rb, line 1314
1314:     def a(href = "") # :yield:
1315:       attributes = if href.kind_of?(String)
1316:                      { "HREF" => href }
1317:                    else
1318:                      href
1319:                    end
1320:       if block_given?
1321:         super(attributes){ yield }
1322:       else
1323:         super(attributes)
1324:       end
1325:     end

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

[Source]

      # File lib/cgi.rb, line 1336
1336:     def base(href = "") # :yield:
1337:       attributes = if href.kind_of?(String)
1338:                      { "HREF" => href }
1339:                    else
1340:                      href
1341:                    end
1342:       if block_given?
1343:         super(attributes){ yield }
1344:       else
1345:         super(attributes)
1346:       end
1347:     end

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>

[Source]

      # File lib/cgi.rb, line 1359
1359:     def blockquote(cite = nil)  # :yield:
1360:       attributes = if cite.kind_of?(String)
1361:                      { "CITE" => cite }
1362:                    else
1363:                      cite or ""
1364:                    end
1365:       if block_given?
1366:         super(attributes){ yield }
1367:       else
1368:         super(attributes)
1369:       end
1370:     end

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>

[Source]

      # File lib/cgi.rb, line 1383
1383:     def caption(align = nil) # :yield:
1384:       attributes = if align.kind_of?(String)
1385:                      { "ALIGN" => align }
1386:                    else
1387:                      align or ""
1388:                    end
1389:       if block_given?
1390:         super(attributes){ yield }
1391:       else
1392:         super(attributes)
1393:       end
1394:     end

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)

[Source]

      # File lib/cgi.rb, line 1413
1413:     def checkbox(name = "", value = nil, checked = nil)
1414:       attributes = if name.kind_of?(String)
1415:                      { "TYPE" => "checkbox", "NAME" => name,
1416:                        "VALUE" => value, "CHECKED" => checked }
1417:                    else
1418:                      name["TYPE"] = "checkbox"
1419:                      name
1420:                    end
1421:       input(attributes)
1422:     end

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

[Source]

      # File lib/cgi.rb, line 1467
1467:     def checkbox_group(name = "", *values)
1468:       if name.kind_of?(Hash)
1469:         values = name["VALUES"]
1470:         name = name["NAME"]
1471:       end
1472:       values.collect{|value|
1473:         if value.kind_of?(String)
1474:           checkbox(name, value) + value
1475:         else
1476:           if value[value.size - 1] == true
1477:             checkbox(name, value[0], true) +
1478:             value[value.size - 2]
1479:           else
1480:             checkbox(name, value[0]) +
1481:             value[value.size - 1]
1482:           end
1483:         end
1484:       }.to_s
1485:     end

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

[Source]

      # File lib/cgi.rb, line 1509
1509:     def file_field(name = "", size = 20, maxlength = nil)
1510:       attributes = if name.kind_of?(String)
1511:                      { "TYPE" => "file", "NAME" => name,
1512:                        "SIZE" => size.to_s }
1513:                    else
1514:                      name["TYPE"] = "file"
1515:                      name
1516:                    end
1517:       attributes["MAXLENGTH"] = maxlength.to_s if maxlength
1518:       input(attributes)
1519:     end

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>

[Source]

      # File lib/cgi.rb, line 1543
1543:     def form(method = "post", action = script_name, enctype = "application/x-www-form-urlencoded")
1544:       attributes = if method.kind_of?(String)
1545:                      { "METHOD" => method, "ACTION" => action,
1546:                        "ENCTYPE" => enctype } 
1547:                    else
1548:                      unless method.has_key?("METHOD")
1549:                        method["METHOD"] = "post"
1550:                      end
1551:                      unless method.has_key?("ENCTYPE")
1552:                        method["ENCTYPE"] = enctype
1553:                      end
1554:                      method
1555:                    end
1556:       if block_given?
1557:         body = yield
1558:       else
1559:         body = ""
1560:       end
1561:       if @output_hidden
1562:         body += @output_hidden.collect{|k,v|
1563:           "<INPUT TYPE=\"HIDDEN\" NAME=\"#{k}\" VALUE=\"#{v}\">"
1564:         }.to_s
1565:       end
1566:       super(attributes){body}
1567:     end

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

[Source]

      # File lib/cgi.rb, line 1584
1584:     def hidden(name = "", value = nil)
1585:       attributes = if name.kind_of?(String)
1586:                      { "TYPE" => "hidden", "NAME" => name, "VALUE" => value }
1587:                    else
1588:                      name["TYPE"] = "hidden"
1589:                      name
1590:                    end
1591:       input(attributes)
1592:     end

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

[Source]

      # File lib/cgi.rb, line 1636
1636:     def html(attributes = {}) # :yield:
1637:       if nil == attributes
1638:         attributes = {}
1639:       elsif "PRETTY" == attributes
1640:         attributes = { "PRETTY" => true }
1641:       end
1642:       pretty = attributes.delete("PRETTY")
1643:       pretty = "  " if true == pretty
1644:       buf = ""
1645: 
1646:       if attributes.has_key?("DOCTYPE")
1647:         if attributes["DOCTYPE"]
1648:           buf += attributes.delete("DOCTYPE")
1649:         else
1650:           attributes.delete("DOCTYPE")
1651:         end
1652:       else
1653:         buf += doctype
1654:       end
1655: 
1656:       if block_given?
1657:         buf += super(attributes){ yield }
1658:       else
1659:         buf += super(attributes)
1660:       end
1661: 
1662:       if pretty
1663:         CGI::pretty(buf, pretty)
1664:       else
1665:         buf
1666:       end
1667: 
1668:     end

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.

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

[Source]

      # File lib/cgi.rb, line 1685
1685:     def image_button(src = "", name = nil, alt = nil)
1686:       attributes = if src.kind_of?(String)
1687:                      { "TYPE" => "image", "SRC" => src, "NAME" => name,
1688:                        "ALT" => alt }
1689:                    else
1690:                      src["TYPE"] = "image"
1691:                      src["SRC"] ||= ""
1692:                      src
1693:                    end
1694:       input(attributes)
1695:     end

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

[Source]

      # File lib/cgi.rb, line 1711
1711:     def img(src = "", alt = "", width = nil, height = nil)
1712:       attributes = if src.kind_of?(String)
1713:                      { "SRC" => src, "ALT" => alt }
1714:                    else
1715:                      src
1716:                    end
1717:       attributes["WIDTH"] = width.to_s if width
1718:       attributes["HEIGHT"] = height.to_s if height
1719:       super(attributes)
1720:     end

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>

[Source]

      # File lib/cgi.rb, line 1737
1737:     def multipart_form(action = nil, enctype = "multipart/form-data")
1738:       attributes = if action == nil
1739:                      { "METHOD" => "post", "ENCTYPE" => enctype } 
1740:                    elsif action.kind_of?(String)
1741:                      { "METHOD" => "post", "ACTION" => action,
1742:                        "ENCTYPE" => enctype } 
1743:                    else
1744:                      unless action.has_key?("METHOD")
1745:                        action["METHOD"] = "post"
1746:                      end
1747:                      unless action.has_key?("ENCTYPE")
1748:                        action["ENCTYPE"] = enctype
1749:                      end
1750:                      action
1751:                    end
1752:       if block_given?
1753:         form(attributes){ yield }
1754:       else
1755:         form(attributes)
1756:       end
1757:     end

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

[Source]

      # File lib/cgi.rb, line 1779
1779:     def password_field(name = "", value = nil, size = 40, maxlength = nil)
1780:       attributes = if name.kind_of?(String)
1781:                      { "TYPE" => "password", "NAME" => name,
1782:                        "VALUE" => value, "SIZE" => size.to_s }
1783:                    else
1784:                      name["TYPE"] = "password"
1785:                      name
1786:                    end
1787:       attributes["MAXLENGTH"] = maxlength.to_s if maxlength
1788:       input(attributes)
1789:     end

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

  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>

[Source]

      # File lib/cgi.rb, line 1834
1834:     def popup_menu(name = "", *values)
1835: 
1836:       if name.kind_of?(Hash)
1837:         values   = name["VALUES"]
1838:         size     = name["SIZE"].to_s if name["SIZE"]
1839:         multiple = name["MULTIPLE"]
1840:         name     = name["NAME"]
1841:       else
1842:         size = nil
1843:         multiple = nil
1844:       end
1845: 
1846:       select({ "NAME" => name, "SIZE" => size,
1847:                "MULTIPLE" => multiple }){
1848:         values.collect{|value|
1849:           if value.kind_of?(String)
1850:             option({ "VALUE" => value }){ value }
1851:           else
1852:             if value[value.size - 1] == true
1853:               option({ "VALUE" => value[0], "SELECTED" => true }){
1854:                 value[value.size - 2]
1855:               }
1856:             else
1857:               option({ "VALUE" => value[0] }){
1858:                 value[value.size - 1]
1859:               }
1860:             end
1861:           end
1862:         }.to_s
1863:       }
1864: 
1865:     end

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.

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

[Source]

      # File lib/cgi.rb, line 1883
1883:     def radio_button(name = "", value = nil, checked = nil)
1884:       attributes = if name.kind_of?(String)
1885:                      { "TYPE" => "radio", "NAME" => name,
1886:                        "VALUE" => value, "CHECKED" => checked }
1887:                    else
1888:                      name["TYPE"] = "radio"
1889:                      name
1890:                    end
1891:       input(attributes)
1892:     end

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

[Source]

      # File lib/cgi.rb, line 1922
1922:     def radio_group(name = "", *values)
1923:       if name.kind_of?(Hash)
1924:         values = name["VALUES"]
1925:         name = name["NAME"]
1926:       end
1927:       values.collect{|value|
1928:         if value.kind_of?(String)
1929:           radio_button(name, value) + value
1930:         else
1931:           if value[value.size - 1] == true
1932:             radio_button(name, value[0], true) +
1933:             value[value.size - 2]
1934:           else
1935:             radio_button(name, value[0]) +
1936:             value[value.size - 1]
1937:           end
1938:         end
1939:       }.to_s
1940:     end

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

[Source]

      # File lib/cgi.rb, line 1957
1957:     def reset(value = nil, name = nil)
1958:       attributes = if (not value) or value.kind_of?(String)
1959:                      { "TYPE" => "reset", "VALUE" => value, "NAME" => name }
1960:                    else
1961:                      value["TYPE"] = "reset"
1962:                      value
1963:                    end
1964:       input(attributes)
1965:     end
scrolling_list(name = "", *values)

Alias for popup_menu

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

[Source]

      # File lib/cgi.rb, line 1987
1987:     def submit(value = nil, name = nil)
1988:       attributes = if (not value) or value.kind_of?(String)
1989:                      { "TYPE" => "submit", "VALUE" => value, "NAME" => name }
1990:                    else
1991:                      value["TYPE"] = "submit"
1992:                      value
1993:                    end
1994:       input(attributes)
1995:     end

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

[Source]

      # File lib/cgi.rb, line 2019
2019:     def text_field(name = "", value = nil, size = 40, maxlength = nil)
2020:       attributes = if name.kind_of?(String)
2021:                      { "TYPE" => "text", "NAME" => name, "VALUE" => value,
2022:                        "SIZE" => size.to_s }
2023:                    else
2024:                      name["TYPE"] = "text"
2025:                      name
2026:                    end
2027:       attributes["MAXLENGTH"] = maxlength.to_s if maxlength
2028:       input(attributes)
2029:     end

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)

[Source]

      # File lib/cgi.rb, line 2045
2045:     def textarea(name = "", cols = 70, rows = 10)  # :yield:
2046:       attributes = if name.kind_of?(String)
2047:                      { "NAME" => name, "COLS" => cols.to_s,
2048:                        "ROWS" => rows.to_s }
2049:                    else
2050:                      name
2051:                    end
2052:       if block_given?
2053:         super(attributes){ yield }
2054:       else
2055:         super(attributes)
2056:       end
2057:     end

[Validate]