[Zope] Dynamically render DTML?
Mark N. Gibson
Mon, 18 Sep 2000 16:55:10 -0600
Thanks too all who replied, I may have simplified my problem a little too
much before posting it.
The heart of it is that I'd like to have the Zope evaluate an arbitrary string
as if it were a DTMLMethod
For those interested, here is what I ended up doing...
from OFS.DTMLMethod import DTMLMethod
""" a callable object:
This is acting as a base class for a ZClass """
""" a callable object - I pass in REQUEST because my dtml-vary seems to
require it """
# create your dtml string - note: the idea is, this doesn't have to be a
text= '<dtml-vary lang="en,en-US,en-BR">\
THIS IS ENGLISH CONTENT\
THSI IS FRENCH CONTENT\
This is spanish text\
# populate the DTMLMethod
meth.manage_edit( text,'THIS IS THE TITLE')
#the DTMLMethod goes out of scope here, so it's never persistent ( I hope
I call this from from zope as follows...
<dtml-comment> m1 is an instance of a ZClass that has myclass as a base
<dtml-comment> this calls myclass.__call__(self,REQUEST) <dtml-comment>
This solution worked for me. It would still be nice if I didn't have to pass
REQUEST explicitly. I could have derived myclass (or the ZClass) from
DTMLMethod, but I didn't need the DTMLMethod itself to be persistent.
Also, I'm not sure how to intercept __call__ and pas it on to the DTMLMethod.
It's a little ugly, but it works. It would be nice to be able to just say
<dtml-var myclassObjectName> in zope and have it render.
Kapil Thangavelu wrote:
> "Mark N. Gibson" wrote:
> > Is it possible to return dtml from python, and have it rendered correctly
> > on the page?
> > for example, the python might look like this...
> > def get_header(somevar=1):
> > if somevar==1:
> > return '<dtml-var standard_html_header>'
> > else:
> > return '<dtml-var other_html_header>'
> > The dtml document in the zodb might look like this...
> > <dtml-var expr="get_header(1)">
> > Body text....
> > <dtml-var standard_html_footer>
> > When this gets rendered, it's as if the dtml document had
> > <dtml-var standard_html_header> in place of the get_header call.
> to have your example work would require multiple or nested passes of the
> dtml-evalutation machinery. so no you can't return a string containing
> dtml and expect it to be evalutated. the machinery expects you to return
> string or value (for dtml-var) to which it will apply formatting rules.
> on the web the evalutation machinery automatically calls (possibly
> nested) dtml tags, so this is not a concern, but again it only performs
> the evaluation once.
> if you want to achieve this result from python you can evaluate/call the
> standard_html_header in python and return the resultant string. see the
> arguements for DTMLMethod in the /lib/python/OFS folder for exact
> doing it from a dtml method would be easier assuming it fits the usage.
> <dtml-call "REQUEST.set('somevar', getvaluefromsomewhere()">
> <dtml-if "somevar==1">
> <dtml-var standard_html_header>
> <dtml-var other_html_header>
> > I understand how to call external methods from dtml, etc. I just want
> > to know if it's possible to get the string returned rendered as dtml.
> > Mark
> > ---------------
> > Mark Gibson
> > Kaivo, Inc.
> > www.kaivo.com
> > _______________________________________________
> > Zope maillist - Zope@zope.org
> > http://lists.zope.org/mailman/listinfo/zope
> > ** No cross posts or HTML encoding! **
> > (Related lists -
> > http://lists.zope.org/mailman/listinfo/zope-announce
> > http://lists.zope.org/mailman/listinfo/zope-dev )
> Zope maillist - Zope@zope.org
> ** No cross posts or HTML encoding! **
> (Related lists -
> http://lists.zope.org/mailman/listinfo/zope-dev )