CGI
contains a huge number of methods used to create HTML---one
method per tag. In order to enable these methods, you must create a
CGI
object by calling
CGI.new
, passing in the required level
of HTML. For these examples, we'll use ``
html3
''.
To make tag nesting easier, these methods take their content as code
blocks. The code blocks should return a
String
, which will be
used as the content for the tag. For this example, we've added some
gratuitous newlines to make the output fit on the page.
require "cgi"
cgi = CGI.new("html3") # add HTML generation methods
cgi.out{
cgi.html{
cgi.head{ "\n"+cgi.title{"This Is a Test"} } +
cgi.body{ "\n"+
cgi.form{"\n"+
cgi.hr +
cgi.h1 { "A Form: " } + "\n"+
cgi.textarea("get_text") +"\n"+
cgi.br +
cgi.submit
}
}
}
}
|
produces:
Content-Type: text/html
Content-Length: 302
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"><HTML><HEAD>
<TITLE>This Is a Test</TITLE></HEAD><BODY>
<FORM METHOD="post" ENCTYPE="application/x-www-form-urlencoded">
<HR><H1>A Form: </H1>
<TEXTAREA NAME="get_text" ROWS="10" COLS="70"></TEXTAREA>
<BR><INPUT TYPE="submit"></FORM></BODY></HTML>
|
This code will produce an HTML form titled ``This Is a Test,''
followed by a horizontal rule, a level-one header, a test input area,
and finally a submit button. When the submit comes back, you'll have
a CGI parameter named ``
get_text
'' containing the text the
user entered.