Previous Next blank.gif Home Page

Class TemplateDocument - Generate documents based on a template and a substitution mapping.


SYNOPSIS

import HTMLgen
class TemplateDocument
  def __init__(self, templatefile, substitutions={})
  string __module__ = 'HTMLgen'
  def __str__(self)
  def sub(self, source, subs=None)
  def write(self, filename = None)

DESCRIPTION

Must use Python 1.5 or newer. Uses re and the get method on dictionaries.

Usage: T = TemplateDocument('Xfile') T.substitutions = {'month': ObjectY, town : 'Scarborough'} T.write('Maine.html')

A dictionary, or object that behaves like a dictionary, is assigned to the substitutions attribute which has symbols as keys to objects. Upon every occurance of these symbols surrounded by braces {} in the source template, the corresponding value is converted to a string and substituted in the output.

For example, source text which looks like: I lost my heart at {town} Fair. becomes: I lost my heart at Scarborough Fair.

Symbols in braces which do not correspond to a key in the dictionary remain unchanged.

def sub(self, source, subs=None)

Perform source text substitutions.

*source* string containing template source text subs mapping of symbols to replacement values

def write(self, filename = None)

Emit the Document HTML to a file or standard output.

Will not overwrite file is it exists and is textually the same. In Unix you can use environment variables in filenames. Will print to stdout if no argument given.

SEE ALSO

HTMLgen


Previous Next blank.gif Home Page
Buzz.gif

Copyright © Robin Friedrich
All Rights Reserved
Comments to author: friedrich@pythonpros.com
Generated: Thu May 28 1998