Ruby CGI實用方法
CGI類方法:
這裡是一個CGI類方法列表:
SN | 說明方法 |
---|---|
1 |
CGI::new([ level="query"]) Creates a CGI object. level may be one of the following options. If one of the HTML levels is specified, the following methods are defined for generating output conforming to that level:
|
2 |
CGI::escape( str) Escapes an unsafe string using URL-encoding. |
3 |
CGI::unescape( str) Expands a string that has been escaped using URL-encoding. |
4 |
CGI::escapeHTML( str) Escapes HTML special characters, including: & < >. |
5 |
CGI::unescapeHTML( str) Expands escaped HTML special characters, including: & < >. |
6 |
CGI::escapeElement( str[, element...]) Escapes HTML special characters in the specified HTML elements. |
7 |
CGI::unescapeElement( str, element[, element...]) Expands escaped HTML special characters in the specified HTML elements. |
8 |
CGI::parse( query) Parses the query and returns a hash containing its key-value pairs. |
9 |
CGI::pretty( string[, leader=" "]) Returns a neatly formatted version of the HTML string. If leader is specified, it's written at the beginning of each line. The default value for leader is two spaces. |
10 |
CGI::rfc1123_date( time) Formats the data and time according to RFC-1123 (for example, Tue, 2 Jun 2008 00:00:00 GMT). |
CGI實例方法:
假設c是由 CGI::new 創建的實例。現在,這裡是一個列表的方法可以應用到這個實例:
SN | 說明方法 |
---|---|
1 |
c[ name] Returns an array containing the value of the field name corresponding to name. |
2 |
c.checkbox( name[, value[, check=false]]) c.checkbox( options) Returns an HTML string defining a checkbox field. Tag attributes may be specified in a hash passed as an argument. |
3 |
c.checkbox_group( name, value...) c.checkbox_group( options) Returns an HTML string defining a checkbox group. Tag attributes may be specified in a hash passed as an argument. |
4 |
c.file_field( name[, size=20[, max]]) c.file_field( options) Returns an HTML string defining a file field. |
5 |
c.form([ method="post"[, url]]) { ...} c.form( options) Returns an HTML string defining a form. If a block is specified, the string produced by its output creates the contents of the form. Tag attributes may be specified in a hash passed as an argument. |
6 |
c.cookies Returns a hash containing a CGI::Cookie object containing keys and values from a cookie. |
7 |
c.header([ header]) Returns a CGI header containing the information in header. If header is a hash, its key-value pairs are used to create the header. |
8 |
c.hidden( name[, value]) c.hidden( options) Returns an HTML string defining a HIDDEN field. Tag attributes may be specified in a hash passed as an argument. |
9 |
c.image_button( url[, name[, alt]]) c.image_button( options) Returns an HTML string defining an image button. Tag attributes may be specified in a hash passed as an argument. |
10 |
c.keys Returns an array containing the field names from the form. |
11 |
c.key?( name) c.has_key?( name) c.include?( name) Returns true if the form contains the specified field name. |
12 |
c.multipart_form([ url[, encode]]) { ...} c.multipart_form( options) { ...} Returns an HTML string defining a multipart form. If a block is specified, the string produced by its output creates the contents of the form. Tag attributes may be specified in a hash passed as an argument. |
13 |
c.out([ header]) { ...} Generates HTML output. Uses the string produced by the block's output to create the body of the page. |
14 |
c.params Returns a hash containing field names and values from the form. |
15 |
c.params= hash Sets field names and values in the form using a hash. |
16 |
c.password_field( name[, value[, size=40[, max]]]) c.password_field( options) Returns an HTML string defining a password field. Tag attributes may be specified in a hash passed as an argument. |
17 |
c.popup_menu( name, value...) c.popup_menu( options) c.scrolling_list( name, value...) c.scrolling_list( options) Returns an HTML string defining a pop-up menu. Tag attributes may be specified in a hash passed as an argument. |
18 |
c.radio_button( name[, value[, checked=false]]) c.radio_button( options) Returns an HTML string defining a radio button. Tag attributes may be specified in a hash passed as an argument. |
19 |
c.radio_group( name, value...) c.radio_group( options) Returns an HTML string defining a radio button group. Tag attributes may be specified in a hash passed as an argument. |
20 |
c.reset( name[, value]) c.reset( options) Returns an HTML string defining a reset button. Tag attributes may be specified in a hash passed as an argument. |
21 |
c.text_field( name[, value[, size=40[, max]]]) c.text_field( options) Returns an HTML string defining a text field. Tag attributes may be specified in a hash passed as an argument. |
22 |
c.textarea( name[, cols=70[, rows=10]]) { ...} c.textarea( options) { ...} Returns an HTML string defining a text area. If a block is specified, the string produced by its output creates the contents of the text area. Tag attributes may be specified in a hash passed as an argument. |
HTML生成方法:
可以創建任何HTML標簽,通過使用相應的HTML標記名具有任何CGI實例。例如:
#!/usr/bin/ruby require "cgi" cgi = CGI.new("html4") cgi.out{ cgi.html{ cgi.head{ " "+cgi.title{"This Is a Test"} } + cgi.body{ " "+ cgi.form{" "+ cgi.hr + cgi.h1 { "A Form: " } + " "+ cgi.textarea("get_text") +" "+ cgi.br + cgi.submit } } } }
CGI對象屬性:
可以訪問任何使用CGI實例以下屬性:
屬性 | 返回值 |
---|---|
accept | Acceptable MIME type |
accept_charset | Acceptable character set |
accept_encoding | Acceptable encoding |
accept_language | Acceptable language |
auth_type | Authentication type |
raw_cookie | Cookie data (raw string) |
content_length | Content length |
content_type | Content type |
From | Client email address |
gateway_interface | CGI version string |
path_info | Extra path |
path_translated | Converted extra path |
Query_string | Query string |
referer | Previously accessed URL |
remote_addr | Client host address |
remote_host | Client hostname |
remote_ident | Client name |
remote_user | Authenticated user |
request_method | Request method (GET, POST, etc.) |
script_name | Program name |
server_name | Server name |
server_port | Server port |
server_protocol | Server protocol |
server_software | Server software |
user_agent | User agent |