Module servlet :: Class HTMLPage
[show private | hide private]
[frames | no frames]

Class HTMLPage

Servlet --+
          |
         HTMLPage


HTMLPage is the servlet that most developers wanting to generate HTML will want to subclass. The respond method of HTMLPage calls write_html which writes to the client a well-defined HTML document.

Many methods and instance variables offer features to the developer to allow the customization of their content. Use of subclassing can produce site-wide continuity of content; e.g., you can create a servlet called SitePage (that inherits from HTMLPage) that will provide your site-wide look and feel, then individual servlets can inherit from SitePage.

To see how HTMLPage writes out an HTML document, look at write_html and the methods it calls.
Method Summary
  respond(self)
This first calls Servlet.repsond (to see if a method should be called; see ok_methods_to_call) and if it returns False, calls write_html.
  write_base(self)
Write BASE tag to the client in the HEAD.
  write_body(self)
Writes the BODY section to the client.
  write_body_parts(self)
Write the content of the BODY section.
  write_content(self)
This method must be overridden to produce content on the page.
  write_css(self)
Writes CSS to the client in the HEAD.
  write_doctype(self)
Writes to the client the <!DOCTYPE...> tag.
  write_head(self)
This writes the HEAD section of the HTML document to the client.
  write_head_parts(self)
This method calls, in order:
  write_html(self)
This method produces a well-formed HTML document and writes it to the client.
  write_js(self)
Writes javascript to the client in the HEAD.
  write_meta(self)
Writes META tags to the client in the HEAD.
  write_shortcut_icon(self)
Writes a LINK tag to the client specifying the shortcut icon.
  write_title(self)
Writes the TITLE tag to the client.
    Inherited from Servlet
  __init__(self)
Base constructor for all servlets.
  __str__(self)
Return a simple string representing an instance:
  add_cookie(self, cookie, value, **kw)
Wrapper around mod_python.Cookie.add_cookie().
  auth(self)
Basic HTTP authentication method.
  count(klass)
Get the count of requests for this servlet. (Class method)
  external_redirect(self, uri, permanently)
Send a redirect to the client via a Location HTTP header.
  flush(self)
Utilily method which immediately flushes all buffered output to the client.
  get_cookies(self, klass, **kw)
Wrapper around mod_python.Cookie.get_cookies().
  internal_redirect(self, uri)
Redirect internally.
  log(self, msg)
Utility method to write a message to the apache error log.
  prep(self)
This is the second user method called by the handler, after auth, prior to respond.
  raw_write(self, *args)
Like write, but all output is immediately flushed to the client and no string coercion is attempted on the arguments.
  sourcefilename(klass)
Get the filename of the source file for this servlet. (Class method)
  wrapup(self)
This is the fourth user method called by the handler, immediately after calling respond, before _finally.
  write(self, *args)
Utility method to write the arguments to the client.
  writeln(self, *args)
Like write, but in addition appends a newline to the output.

Instance Variable Summary
str base: If set, this will specify the BASE tag in the HEAD.
dict body_attrs: Attributes for BODY tag.
str or None content_type: Specifies the content type of the servlet, i.e., "text/html".
str css: A string or list of strings of CSS.
list css_links: A list of hrefs (strings).
str doctype: See write_doctype
one of "strict", "loose", "frameset" dtd: See write_doctype
dict http_equiv: http_equiv, like meta, produces META tags in the HEAD, but instead of the keys being the value of the name attribute, they are the value of the http-equiv attribute.
str js: A string or list of strings of javascript.
list js_src: A list of hrefs (strings).
dict meta: A dict, which if non-empty will produce META tags in the HEAD.
str shortcut_icon: If non empty, specifies the href to a shortcut icon and produces a LINK tag in the HEAD:
str or callable title: The title of the HTML page.
    Inherited from Servlet
str auth_realm: Specifies the realm for basic HTTP authentication.
instance of mod_python.util.FieldStorage form: User data sent with request via form or url.
list form_vars: This is the same as query_vars except these variables are only processed for POST requests.
float instantiated: Timestamp of when the servlet was instantiated.
list of unbound methods ok_methods_to_call: list of methods that can be called directly via a POST.
list of str path_info: req.path_info canonicalized as a list: stripping beginning and trailing "/" and splitting it on internal "/".
list query_vars: List of arguments to be searched for in form to be set as instance variables of the servlet.
  req: The apache request object.
bool reusable: Flag (default: True) indicating whether or not an instance of this servlet can be used for multiple requests.
instance of mod_python.Session.Session or None session: A Session (see the mod_python documentation for mod_python.Session.Session).
int session_timeout: Length of time, in seconds, until session times out.
bool use_session: If true, create (or reload) session for each request.

Instance Method Details

respond(self)

This first calls Servlet.repsond (to see if a method should be called; see ok_methods_to_call) and if it returns False, calls write_html.
Overrides:
servlet.Servlet.respond

write_base(self)

Write BASE tag to the client in the HEAD.

See Also: base

write_body(self)

Writes the BODY section to the client.

This method writes "<BODY...>" to the client taking into account the body_attrs instance variable. It then calls write_body_parts and finally writes the "</BODY>" tag.

This method will not be typically overridden. To modify the content of the BODY section, see write_body_parts.

write_body_parts(self)

Write the content of the BODY section.

The base implementation simply calls write_content.

For complex pages that have many sections, this method will typically be overridden, e.g., write to the client the layout of the site look and feel and where page-specific content should appear, call write_content.

See the tutorial that comes with the distribution for examples of how this method can be used to create site-wide content.

write_content(self)

This method must be overridden to produce content on the page.

write_css(self)

Writes CSS to the client in the HEAD.
See Also:
css_links, css

write_doctype(self)

Writes to the client the <!DOCTYPE...> tag. By default, the following is used:
   <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
             "http://www.w3.org/TR/html4/loose.dtd">        

This can be controlled by setting the doctype instance variable. If set, it will be written, as-is, to the client. If it is not set, then the dtd instance variable will be checked; it can be one of "strict", "loose" or "frameset". The default is "loose" and the resulting DOCTYPE is shown above. If set to "strict" or "frameset" an appropriate DOCTYPE will be generated.

This method will not be typically overridden.

write_head(self)

This writes the HEAD section of the HTML document to the client.

It first writes "<HEAD"> then calls write_head_parts and, lastly, writes "</HEAD>".

This method will not be typically overridden. To modify the content of the HEAD section, see write_head_parts.

write_head_parts(self)

This method calls, in order:
  1. write_title
  2. write_shortcut_icon
  3. write_base
  4. write_meta
  5. write_css
  6. write_js

See the above methods for details about what each writes to the client. All of the above methods can have the content they write to the client controlled by setting (or unsetting) instance variables.

While the base method will serve most developer needs, this method is a likely candidate to be overridden. If you want to add to the HEAD you will likely want to call the superclass method and then write additional content in your method, e.g.:
   class MyServlet(HTMLPage):
      ...

      def write_head_parts(self):
          HTMLPage.write_head_parts(self)
          # add my own content
          self.writeln(...)

write_html(self)

This method produces a well-formed HTML document and writes it to the client.

It first calls write_doctype which writes the DOCTYPE. It then writes "<HTML>". In turn it calls write_head and write_body which write the HEAD and BODY sections, respectively. It finishes by writing "</HTML>".
Returns:
True

write_js(self)

Writes javascript to the client in the HEAD.
See Also:
js_src, js

write_meta(self)

Writes META tags to the client in the HEAD.
See Also:
meta, http_equiv

write_shortcut_icon(self)

Writes a LINK tag to the client specifying the shortcut icon.

See Also: shortcut_icon

write_title(self)

Writes the TITLE tag to the client.

It checks for the title instance variable. If it does not exist it uses the name of the servlet for the title. If title is callable it will be called (with no arguments) and its output will be used as the title.

See Also: title


Instance Variable Details

base

If set, this will specify the BASE tag in the HEAD. If base is a string it will be the value of the href attribute. If base is a tuple, it should have two string elements of the form (href, target) which will be the values of those BASE tag attributes.
Type:
str
Value:
''                                                                     

body_attrs

Attributes for BODY tag. Default is empty dict. If non empty, the keys will become attribute names for the BODY tag and the values will be the attribute values.
Type:
dict
Value:
{}                                                                     

content_type

Specifies the content type of the servlet, i.e., "text/html". If it is not set (None) then it defaults to "text/plain". Default: None
Type:
str or None
Value:
'text/html'                                                            

css

A string or list of strings of CSS. If non empty, a <STYLE type="text/css">...</STYLE> section will be placed in the HEAD. If css is a list of strings, the elements will be joined (seperated by newlines) and placed in a single <STYLE ...>...</STYLE> section.
Type:
str
Value:
''                                                                     

css_links

A list of hrefs (strings). For each href, a <LINK type="text/css" rel="stylesheet" ...> will be placed in the HEAD.
Type:
list
Value:
[]                                                                     

doctype

See write_doctype
Type:
str

dtd

See write_doctype
Type:
one of "strict", "loose", "frameset"

http_equiv

http_equiv, like meta, produces META tags in the HEAD, but instead of the keys being the value of the name attribute, they are the value of the http-equiv attribute. See meta for details.
Type:
dict
Value:
{}                                                                     

js

A string or list of strings of javascript. If non empty, a <SCRIPT type="text/javascript">...</SCRIPT> section will be placed in the HEAD. If js is a list of strings, the elements will be joined (seperated by newlines) and placed in a single <SCRIPT ...>...</SCRIPT> section.
Type:
str
Value:
''                                                                     

js_src

A list of hrefs (strings). For each href, a <SCRIPT type="text/javascript" src="HREF"></SCRIPT> will be placed in the HEAD.
Type:
list
Value:
[]                                                                     

meta

A dict, which if non-empty will produce META tags in the HEAD. The keys of the dict will be the values of the name attribute and their values will become the content attribute. For example, the following value of meta:
  meta = {"Author" : "Daniel Popowich",
          "Date"   : "April 28, 2004"}
will produce the following output in the HEAD:
  <META name="Author" content="Daniel Popowich">
  <META name="Date" content="April 28, 2004">
The values of the dict may be a list or tuple of strings, in which case multiple META tags will be produced for the same name attribute. For example:
  meta = {"Author" : ["Tom", "Jerry"]}
will produce the following output in the HEAD:
  <META name="Author" content="Tom">
  <META name="Author" content="Jerry">
Type:
dict
Value:
{}                                                                     

shortcut_icon

If non empty, specifies the href to a shortcut icon and produces a LINK tag in the HEAD:

<LINK rel="shortcut icon" href="shortcut_icon">
Type:
str
Value:
''                                                                     

title

The title of the HTML page. If not set, it defaults to the name of the servlet. title can be a callable that accepts no arguments, in which case the title will be the output of the callable.
Type:
str or callable

Generated by Epydoc 2.0 on Tue Mar 15 08:20:22 2005 http://epydoc.sf.net