3.1 Hello World

The ``hello world'' example provides the simplest application of Crawdad's HTML generator.

import crawdad

class HelloWorldDoc (crawdad.HtmlDocument):

    def mainBody (self):
        return self.P ("Hello world.")

Subclass HelloWorldDoc inherits lots of behavior from HtmlDocument. Crawdad's HtmlDocument includes a model for a well formed HTML document: a DOCTYPE definition followed by an <HTML> entity, which contains a <HEAD> with at least a <TITLE> and a <BODY>.

Subclasses like HelloWorldDoc can override methods like to define custom content. In this example, HelloWorldDoc redefines mainBody to include custom content--a <P> (paragraph) containing the words ``Hello world.'' The inherited P method creates an HtmlElement object initialized to generate a <P> with the contents ``Hello world.''

To generate the HTML document and print it to standard output, create an instance of HelloWorldDoc and call its printHtml method.

>>> doc = HelloWorldDoc ()
>>> doc.printHtml ()
<HTML><HEAD><TITLE>Untitled document</TITLE>
<BODY><P>Hello world.</P>

The resulting HTML document is structurally correct. It includes a document type definition; HTML, HEAD, and BODY tags; and a default title. All tags are correctly rendered, nested, and closed.