Previous Next blank.gif blank.gif

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


SYNOPSIS

import HTMLgen
class AutoTemplateDocument(AutoStringTemplate)
  string __module__ = 'HTMLgen'
  def set_template(self, template)

  # Methods inherited by AutoTemplateDocument from AutoStringTemplate
  def extract_template(self, source=None)

  # Methods inherited by AutoTemplateDocument from StringTemplate
  def __getitem__(self, name)
  def __init__(self, template, substitutions=None, **kw)
  def __setitem__(self, name, value)
  def __str__(self)
  def keys(self)
  def write(self, filename = None)

DESCRIPTION

The primary difference between AutoTemplateDocument and TemplateDocument is that the Auto version can read through an HTML file previously generated with this class and identify the regions of text that were substituted. It then extracts that text into the substitution dictionary and can then be updated. The intent is to eliminate the need to maintain separate content files for insertion into templates. The HTML output file can double as a template for future use. Output from this class have their filled regions marked by comments: ...gets <!--{wz}Begin-->glued,<!--{wz}End--> in place... Which came from ...gets {wz} in place... in old style template syntax.

AutoTemplateDocument is a functional superset of TemplateDocument and should be compatible.

Usage: T = AutoTemplateDocument('Maine.html') T.substitutions = {'month': ObjectY, town : 'Scarborough'} or T['month'] = ObjectY ; T['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 extract_template(self, source=None)

Convert comment-marked regions to a regular {tokens}.

Updates the substitution dictionary with the text from the region.

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

AutoStringTemplate


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