Document Templating Markup Language (DTML)

5.3 · active · verified Fri Apr 17

DocumentTemplate is a Python library implementing the Document Templating Markup Language (DTML), originally developed for the Zope application server. It allows for dynamic generation of HTML, XML, or other text documents using a tag-based templating system. The current version is 5.3, with releases typically tied to bug fixes or Python version compatibility updates.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to define a DTML template string, prepare a context dictionary, and render the template using the `HTML` class. It includes examples of variable substitution, conditional logic (`dtml-if`), and accessing nested context data like environment variables.

from DocumentTemplate import HTML
import os

template_source = """
<h1>Hello <dtml-var name="user_name">!</h1>
<p>Your lucky number is <dtml-var name="lucky_number">.</p>
<dtml-if expr="lucky_number > 5">
  <p>That's a big number!</p>
<dtml-else>
  <p>That's a small number.</p>
</dtml-if>
<p>Using environment: <dtml-var expr="request.environ.get('TEST_ENV_VAR', 'N/A')"></p>
"""

# Prepare context dictionary
context = {
    'user_name': 'Registry User',
    'lucky_number': 7,
    'request': {
        'environ': {
            'TEST_ENV_VAR': os.environ.get('TEST_ENV_VAR', 'DEFAULT_TEST_VALUE')
        }
    }
}

# Create template instance
template = HTML(template_source)

# Render the template
rendered_output = template.render(**context)
print(rendered_output)

view raw JSON →