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 1311
1311:     def a(href = "") # :yield:
1312:       attributes = if href.kind_of?(String)
1313:                      { "HREF" => href }
1314:                    else
1315:                      href
1316:                    end
1317:       if block_given?
1318:         super(attributes){ yield }
1319:       else
1320:         super(attributes)
1321:       end
1322:     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 1333
1333:     def base(href = "") # :yield:
1334:       attributes = if href.kind_of?(String)
1335:                      { "HREF" => href }
1336:                    else
1337:                      href
1338:                    end
1339:       if block_given?
1340:         super(attributes){ yield }
1341:       else
1342:         super(attributes)
1343:       end
1344:     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 1356
1356:     def blockquote(cite = nil)  # :yield:
1357:       attributes = if cite.kind_of?(String)
1358:                      { "CITE" => cite }
1359:                    else
1360:                      cite or ""
1361:                    end
1362:       if block_given?
1363:         super(attributes){ yield }
1364:       else
1365:         super(attributes)
1366:       end
1367:     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 1380
1380:     def caption(align = nil) # :yield:
1381:       attributes = if align.kind_of?(String)
1382:                      { "ALIGN" => align }
1383:                    else
1384:                      align or ""
1385:                    end
1386:       if block_given?
1387:         super(attributes){ yield }
1388:       else
1389:         super(attributes)
1390:       end
1391:     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 1410
1410:     def checkbox(name = "", value = nil, checked = nil)
1411:       attributes = if name.kind_of?(String)
1412:                      { "TYPE" => "checkbox", "NAME" => name,
1413:                        "VALUE" => value, "CHECKED" => checked }
1414:                    else
1415:                      name["TYPE"] = "checkbox"
1416:                      name
1417:                    end
1418:       input(attributes)
1419:     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 1464
1464:     def checkbox_group(name = "", *values)
1465:       if name.kind_of?(Hash)
1466:         values = name["VALUES"]
1467:         name = name["NAME"]
1468:       end
1469:       values.collect{|value|
1470:         if value.kind_of?(String)
1471:           checkbox(name, value) + value
1472:         else
1473:           if value[value.size - 1] == true
1474:             checkbox(name, value[0], true) +
1475:             value[value.size - 2]
1476:           else
1477:             checkbox(name, value[0]) +
1478:             value[value.size - 1]
1479:           end
1480:         end
1481:       }.to_s
1482:     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 1506
1506:     def file_field(name = "", size = 20, maxlength = nil)
1507:       attributes = if name.kind_of?(String)
1508:                      { "TYPE" => "file", "NAME" => name,
1509:                        "SIZE" => size.to_s }
1510:                    else
1511:                      name["TYPE"] = "file"
1512:                      name
1513:                    end
1514:       attributes["MAXLENGTH"] = maxlength.to_s if maxlength
1515:       input(attributes)
1516:     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 1540
1540:     def form(method = "post", action = script_name, enctype = "application/x-www-form-urlencoded")
1541:       attributes = if method.kind_of?(String)
1542:                      { "METHOD" => method, "ACTION" => action,
1543:                        "ENCTYPE" => enctype } 
1544:                    else
1545:                      unless method.has_key?("METHOD")
1546:                        method["METHOD"] = "post"
1547:                      end
1548:                      unless method.has_key?("ENCTYPE")
1549:                        method["ENCTYPE"] = enctype
1550:                      end
1551:                      method
1552:                    end
1553:       if block_given?
1554:         body = yield
1555:       else
1556:         body = ""
1557:       end
1558:       if @output_hidden
1559:         body += @output_hidden.collect{|k,v|
1560:           "<INPUT TYPE=\"HIDDEN\" NAME=\"#{k}\" VALUE=\"#{v}\">"
1561:         }.to_s
1562:       end
1563:       super(attributes){body}
1564:     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 1581
1581:     def hidden(name = "", value = nil)
1582:       attributes = if name.kind_of?(String)
1583:                      { "TYPE" => "hidden", "NAME" => name, "VALUE" => value }
1584:                    else
1585:                      name["TYPE"] = "hidden"
1586:                      name
1587:                    end
1588:       input(attributes)
1589:     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 1633
1633:     def html(attributes = {}) # :yield:
1634:       if nil == attributes
1635:         attributes = {}
1636:       elsif "PRETTY" == attributes
1637:         attributes = { "PRETTY" => true }
1638:       end
1639:       pretty = attributes.delete("PRETTY")
1640:       pretty = "  " if true == pretty
1641:       buf = ""
1642: 
1643:       if attributes.has_key?("DOCTYPE")
1644:         if attributes["DOCTYPE"]
1645:           buf += attributes.delete("DOCTYPE")
1646:         else
1647:           attributes.delete("DOCTYPE")
1648:         end
1649:       else
1650:         buf += doctype
1651:       end
1652: 
1653:       if block_given?
1654:         buf += super(attributes){ yield }
1655:       else
1656:         buf += super(attributes)
1657:       end
1658: 
1659:       if pretty
1660:         CGI::pretty(buf, pretty)
1661:       else
1662:         buf
1663:       end
1664: 
1665:     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 1682
1682:     def image_button(src = "", name = nil, alt = nil)
1683:       attributes = if src.kind_of?(String)
1684:                      { "TYPE" => "image", "SRC" => src, "NAME" => name,
1685:                        "ALT" => alt }
1686:                    else
1687:                      src["TYPE"] = "image"
1688:                      src["SRC"] ||= ""
1689:                      src
1690:                    end
1691:       input(attributes)
1692:     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 1708
1708:     def img(src = "", alt = "", width = nil, height = nil)
1709:       attributes = if src.kind_of?(String)
1710:                      { "SRC" => src, "ALT" => alt }
1711:                    else
1712:                      src
1713:                    end
1714:       attributes["WIDTH"] = width.to_s if width
1715:       attributes["HEIGHT"] = height.to_s if height
1716:       super(attributes)
1717:     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 1734
1734:     def multipart_form(action = nil, enctype = "multipart/form-data")
1735:       attributes = if action == nil
1736:                      { "METHOD" => "post", "ENCTYPE" => enctype } 
1737:                    elsif action.kind_of?(String)
1738:                      { "METHOD" => "post", "ACTION" => action,
1739:                        "ENCTYPE" => enctype } 
1740:                    else
1741:                      unless action.has_key?("METHOD")
1742:                        action["METHOD"] = "post"
1743:                      end
1744:                      unless action.has_key?("ENCTYPE")
1745:                        action["ENCTYPE"] = enctype
1746:                      end
1747:                      action
1748:                    end
1749:       if block_given?
1750:         form(attributes){ yield }
1751:       else
1752:         form(attributes)
1753:       end
1754:     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 1776
1776:     def password_field(name = "", value = nil, size = 40, maxlength = nil)
1777:       attributes = if name.kind_of?(String)
1778:                      { "TYPE" => "password", "NAME" => name,
1779:                        "VALUE" => value, "SIZE" => size.to_s }
1780:                    else
1781:                      name["TYPE"] = "password"
1782:                      name
1783:                    end
1784:       attributes["MAXLENGTH"] = maxlength.to_s if maxlength
1785:       input(attributes)
1786:     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 1831
1831:     def popup_menu(name = "", *values)
1832: 
1833:       if name.kind_of?(Hash)
1834:         values   = name["VALUES"]
1835:         size     = name["SIZE"].to_s if name["SIZE"]
1836:         multiple = name["MULTIPLE"]
1837:         name     = name["NAME"]
1838:       else
1839:         size = nil
1840:         multiple = nil
1841:       end
1842: 
1843:       select({ "NAME" => name, "SIZE" => size,
1844:                "MULTIPLE" => multiple }){
1845:         values.collect{|value|
1846:           if value.kind_of?(String)
1847:             option({ "VALUE" => value }){ value }
1848:           else
1849:             if value[value.size - 1] == true
1850:               option({ "VALUE" => value[0], "SELECTED" => true }){
1851:                 value[value.size - 2]
1852:               }
1853:             else
1854:               option({ "VALUE" => value[0] }){
1855:                 value[value.size - 1]
1856:               }
1857:             end
1858:           end
1859:         }.to_s
1860:       }
1861: 
1862:     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 1880
1880:     def radio_button(name = "", value = nil, checked = nil)
1881:       attributes = if name.kind_of?(String)
1882:                      { "TYPE" => "radio", "NAME" => name,
1883:                        "VALUE" => value, "CHECKED" => checked }
1884:                    else
1885:                      name["TYPE"] = "radio"
1886:                      name
1887:                    end
1888:       input(attributes)
1889:     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 1919
1919:     def radio_group(name = "", *values)
1920:       if name.kind_of?(Hash)
1921:         values = name["VALUES"]
1922:         name = name["NAME"]
1923:       end
1924:       values.collect{|value|
1925:         if value.kind_of?(String)
1926:           radio_button(name, value) + value
1927:         else
1928:           if value[value.size - 1] == true
1929:             radio_button(name, value[0], true) +
1930:             value[value.size - 2]
1931:           else
1932:             radio_button(name, value[0]) +
1933:             value[value.size - 1]
1934:           end
1935:         end
1936:       }.to_s
1937:     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 1954
1954:     def reset(value = nil, name = nil)
1955:       attributes = if (not value) or value.kind_of?(String)
1956:                      { "TYPE" => "reset", "VALUE" => value, "NAME" => name }
1957:                    else
1958:                      value["TYPE"] = "reset"
1959:                      value
1960:                    end
1961:       input(attributes)
1962:     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 1984
1984:     def submit(value = nil, name = nil)
1985:       attributes = if (not value) or value.kind_of?(String)
1986:                      { "TYPE" => "submit", "VALUE" => value, "NAME" => name }
1987:                    else
1988:                      value["TYPE"] = "submit"
1989:                      value
1990:                    end
1991:       input(attributes)
1992:     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 2016
2016:     def text_field(name = "", value = nil, size = 40, maxlength = nil)
2017:       attributes = if name.kind_of?(String)
2018:                      { "TYPE" => "text", "NAME" => name, "VALUE" => value,
2019:                        "SIZE" => size.to_s }
2020:                    else
2021:                      name["TYPE"] = "text"
2022:                      name
2023:                    end
2024:       attributes["MAXLENGTH"] = maxlength.to_s if maxlength
2025:       input(attributes)
2026:     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 2042
2042:     def textarea(name = "", cols = 70, rows = 10)  # :yield:
2043:       attributes = if name.kind_of?(String)
2044:                      { "NAME" => name, "COLS" => cols.to_s,
2045:                        "ROWS" => rows.to_s }
2046:                    else
2047:                      name
2048:                    end
2049:       if block_given?
2050:         super(attributes){ yield }
2051:       else
2052:         super(attributes)
2053:       end
2054:     end

[Validate]