3.2 Your First Albatross Program

#
3.2 Your First Albatross Program

This section rewrites the sample CGI program from the previous section as an Albatross program. The program uses the Albatross templating system to generate the HTML document.

The sample program from this section is supplied in the `samples/templates/simple2` directory and can be installed in your web server `cgi-bin` directory by running the following commands.

    cd samples/templates/simple2
    python install.py

All of the HTML is moved into a template file called `simple.html`.

    <html>
     <head>
      <title>My CGI application</title>
     </head>
     <body>
      Hello from my second simple CGI application!
     </body>
    </html>

The `simple.py` program is then rewritten as shown below.

    #!/usr/bin/python
    from albatross import SimpleContext

    ctx = SimpleContext('.')
    templ = ctx.load_template('simple.html')
    templ.to_html(ctx)

    print 'Content-Type: text/html'
    print
    ctx.flush_content()

You can see the program output by pointing your browser at http://www.object-craft.com.au/cgi-bin/alsamp/simple2/simple.py.

This is probably the most simple application that you can write using Albatross. Let's analyse the program step-by-step.

This first line imports the Albatross package and places the `SimpleContext` into the global namespace.

    from albatross import SimpleContext

Before we can use Albatross templates we must create an execution context which will be used to load and execute the template file. The Albatross `SimpleContext` object should be used in programs which directly load and execute template files. The `SimpleContext` constructor has a single argument which specifies a path to the directory from which template files will be loaded. Before Apache executes a CGI program it sets the current directory to the directory where that program is located. We have installed the template file in the same directory as the program, hence the path `'.'`.

    ctx = SimpleContext('.')

Once we have an execution context we can load template files. The return value of the execution context `load_template()` method is a parsed template.

    templ = ctx.load_template('simple.html')

Albatross templates are executed in two stages; the first stage parses the template and compiles the embedded Python expressions, the second actually executes the template.

To execute a template we call it's `to_html()` method passing an execution context. Albatross tags access application data and logic via the execution context. Since the template for the example application does not refer to any application functionality, we do not need to place anything into the context before executing the template.

    templ.to_html(ctx)

Template file output is accumulated in the execution context.

Unless you use one of the Albatross application objects you need to output your own HTTP headers.

    print 'Content-Type: text/html'
    print

Finally, you must explicitly flush the context to force the HTML to be written to output. Buffering the output inside the context allows applications to trap and handle any exception which occurs while executing the template without any partial output leaking to the browser.

    ctx.flush_content()