Edgewall Software

Changes between Version 33 and Version 34 of MarkupTemplates


Ignore:
Timestamp:
Sep 8, 2006, 10:24:43 AM (18 years ago)
Author:
cmlenz
Comment:

Added back section about the Python API

Legend:

Unmodified
Added
Removed
Modified
  • MarkupTemplates

    v33 v34  
    2626   :depth: 3
    2727.. sectnum::
     28
     29----------
     30Python API
     31----------
     32
     33The Python code required for templating with Markup is generally based on the
     34following pattern:
     35
     36* Attain a ``Template`` object from a string or file object containing the
     37  template XML source. This can either be done directly, or through a
     38  ``TemplateLoader`` instance.
     39* Call the ``generate()`` method of the template, passing any data that should
     40  be made available to the template as keyword arguments.
     41* Serialize the resulting stream using its ``render()`` method.
     42
     43For example::
     44
     45  from markup.template import Template
     46
     47  tmpl = Template('<h1>$title</h1>')
     48  stream = tmpl.generate(title='Hello, world!')
     49  print stream.render('xhtml')
     50
     51That code would produce the following output::
     52
     53  <h1>Hello, world!</h1>
     54
     55However, if you want includes_ to work, you should attain the template instance
     56through a ``TemplateLoader``, and load the template from a file::
     57
     58  from markup.template import TemplateLoader
     59
     60  loader = TemplateLoader([templates_dir])
     61  tmpl = loader.load('test.html')
     62  stream = tmpl.generate(title='Hello, world!')
     63  print stream.render('xhtml')
    2864
    2965