quick search:
 

Render strings with PythonScripts (DTML)

Submitted by: dlkita
Last Edited: 2004-10-14

Category: Python(Script)

Average rating is: 5.0 out of 5 (2 ratings)

Description:
This is a simple way of rendering strings as if
they where DTML from PythonScripts.

Sometimes you might need to generate DTML code or,
as is my case, you store all your content in a external
datastore (like a RDBMS) and oly have access to your
code in textual format.


Source (Text):
# Example code:

# Import DTML from standard class and 
# get the HTML request and response objects.
from Products.PythonScripts.standard import DTML

request = container.REQUEST
RESPONSE =  request.RESPONSE

myString = '<dtml-var standard_html_header>'

# "Coerce" into a DTML-object
myDTML = DTML(myString)

# Return the rendered DTML by *calling* the DTML-object,
# in this case myDTML. context is the namepace - IMPORTANT
return myDTML(context, request, RESPONSE)

# RESPONSE can actually be ommited totally, since it is not used
# and defaults to None.  To restrict the namespace you can use 
# container instead, that way the DTML object will not aq vars
# outside the containing object/folder of this PythonScript

Explanation:
cool, huh? :-)

Comments:

by vernier - 2004-10-14
Thank you **Very Much**; these 3 lines of code have saved me hours of research and helped me gel my understanding of the zen involved in DTML rendering!!!  Bless you for putting this up!