Previous Next blank.gif blank.gif

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


SYNOPSIS

import HTMLgen
class StringTemplate
  def __getitem__(self, name)
  def __init__(self, template, substitutions=None, **kw)
  string __module__ = 'HTMLgen'
  def __setitem__(self, name, value)
  def __str__(self)
  def keys(self)
  def set_template(self, template)
  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.

An optional third argument to the class is a list or two strings to be used as the delimiters instead of { } braces. They must be of the same length; for example "##+ , '##'" is invalid.

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 blank.gif
Buzz.gif

Copyright © Robin Friedrich
All Rights Reserved
Comments to author: friedrich@pythonpros.com
Generated: Tue Apr 20, 1999