[Zope] Dynamically render DTML?

Mark N. Gibson mark@kaivo.com
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...

--------------------------------------
pyfile.py

from OFS.DTMLMethod import DTMLMethod


class myclass:
    """ a callable object:
           This is acting as a base class for a ZClass """


   def __call__(self,REQUEST):
   """    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
static string...
   text= '<dtml-vary lang="en,en-US,en-BR">\
        <H1>ENGLIS TITLE</H1>\
        THIS IS ENGLISH CONTENT\
        <dtml-vary lang="fr,fr-CA,fr-FR">\
        THSI IS FRENCH CONTENT\
        <dtml-vary lang="es">\
        <H1>Spanish title</H1>\
        This is spanish text\
        </dtml-vary>'

   met h=DTMLMethod()
   # 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
).
   return meth(REQUEST)

--------------------------

I call this from from zope as follows...

-------------------------
<dtml-var standard_html_header>
<h2><dtml-var title_or_id></h2>

<p>

<dtml-comment> m1 is an instance of a ZClass that has myclass as a base
class.  <dtml-comment>
<dtml-comment> this   calls myclass.__call__(self,REQUEST)    <dtml-comment>
<dtml-var expr="m1(REQUEST)">


</p>

<dtml-var standard_html_footer>

-------------------------

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.

Mark


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
> a
> 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
> syntax.
>
> 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-else>
>         <dtml-var other_html_header>
> </dtml-if>
>
> Cheers
>
> Kapil
>
> > 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
> 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 )