DTML<->Python confusion (RE: [Zope] How to make a script return rendered dtml)
I've spent hours, maybe days, being confused about this as well. According to the Zope API documentation and ZDP at http://zdp.zope.org/projects/zfaq/faq/DTML/959888072 http://zdp.zope.org/projects/zsnippet/snippets/DTMLTags/CallingDTMLMetho ds someDTMLMethod(_.None, _) should work. Because of the unclear/complex calling conventions and integration between DTML and Python I've had to keep some code in DTML. Other related questions are: How do you do dtml-with and dtml-let in a Python script? (I.e. put something on the namespace) How do you give positional (not keyword) arguments to a DTML method; is there any difference? When is __call__ called, when is __str__ called, and when is index_html called? Other basic functions worth knowing about for a class/object to be a good-behaving Zope object? When does RESPONSE need to be passed; it can always be gotten from the REQUEST, right? Calling a DTML method, when to use client and when to use REQUEST? Is calling with client and REQUEST the same as adding client to the namespace (REQUEST) and calling it with an client=None? Are namespace, _, REQUEST, and context just different names for the same thing? I've been dreaming about a FAQ answering all of this (and a bunch of ZClasses questions I have). Me thinks the ZDP should be integrated with Zope.org, and all Zope.org members given access to add FAQs (I am not allowed to add FAQs now). Ideally, somebody should be adding FAQs all the time, and the FAQ index should be posted regularly to zope-dev... Btw, I think http://zdp.zope.org/projects/zfaq/faq/DTML/959888072 gets the order of aMappingObject and aClient wrong in the first code line. Bye, -- Bjorn -----Original Message----- From: Tim Hicks [mailto:tim@sitefusion.co.uk] Posted At: Thursday, June 14, 2001 00:15 Posted To: Zope List Conversation: [Zope] How to make a script return rendered dtml Subject: Re: [Zope] How to make a script return rendered dtml ----- Original Message ----- From: "Eric Balasbas" <ebalasba@bway.net> To: "Tim Hicks" <tim@sitefusion.co.uk> Cc: <zope@zope.org> Sent: Wednesday, June 13, 2001 4:33 PM Subject: Re: [Zope] How to make a script return rendered dtml
I'm not sure if this is the best way to do it, but if you want to render a DTML method or DTML document inside a Python script, use the syntax:
someDTMLMethod(_.None, _)
I tried this: print context(_.None, _) and got this error: Error Type: NameError Error Value: _ So I tried context.id(_.None,_) and got the same. So I tried 'print context(None,_)', with the same error resulting. Finally, I have tried: print context(None,) and got Error Type: KeyError Error Value: standard_html_header This last error at least seems like it's on the right track as it is parsing through the text of the file. What's going wrong, any idea? thanks tim
Eric Balasbas Senior Developer eric@virtosi.com
http://www.virtosi.com/ Virtosi Ltd. Design -- Branding -- Zope
On Wed, 13 Jun 2001, Tim Hicks wrote:
I have a ZClass (testclass) which subclasses DTMLDocument, and within this zclass, I have a script (python) called index_html. Obviously, this script is called each time an instance of testclass is requested. Based on the presence of a query string, I want to either return the document_src (although with a few modifications), or simply return the rendered instance... by this, I mean have all the dtml tags that are in the instance evaluated. So, my question is, how do I make Zope evaluate the dtml tags?
Here is what I have so far in my script:
------------ if context.REQUEST.QUERY_STRING == 'editor': print context.raw else: print context
return printed ------------
The 'else' statement simply gives me things like,
<dtml-var standard_html_header>
I also tried 'else: render(context)', which was simply a guess on my part, but I get a Zope error as follows:
Error Type: AttributeError Error Value: validate
Can anyone enlighten me?
Cheers
tim
From: "Bjorn Stabell" <bjorn@exoweb.net>
How do you do dtml-with and dtml-let in a Python script? (I.e. put something on the namespace)
You can't. Scripts can use the DTML namespace in the same way that Python expressions in DTML can, but that's it. On the other hand, you can create (and pass along) all the local variables you want.
How do you give positional (not keyword) arguments to a DTML method; is there any difference?
Keyword arguments are put on top of the namespace. You can't use positional arguments, except the standard (None, _) idiom. DTML was never meant to pass arguments around, that's what the namespace is for, for what that's worth :-P
When is __call__ called, when is __str__ called, and when is index_html called?
When an object with a publishable 'index_html' attribute is the target of a request, that attribute is used. Failing that, or if the object is rendered by calling it from DTML or a Script, __call__ is used. DTML's "var" tag will try to convert whatever you give it into a string, so if you give it an object with a __str__ method, it'll get called.
When does RESPONSE need to be passed; it can always be gotten from the REQUEST, right?
Right.
Are namespace, _, REQUEST, and context just different names for the same thing?
Not quite. The namespace, accessible in DTML as "_", is a pile of objects. The REQUEST is a particular object created by ZPublisher to hold information about the request, such as form variables and URL information. The context (or client, for DTML) is the object on which the method was called. If the DTML Method is the target of the request, or if you pass a client as the first argument when calling it, the Method pushes it onto the namespace. Also, the context (like any other Zope object) is acquisition wrapped. The ultimate acquisition parent of any publsihed object is a special wrapper for REQUEST. All this means that when you ask the namespace for the name "foo", it will end up looking for it in the context, in the context's containers all the way to the root, and the REQUEST. Cheers, Evan @ digicool
participants (2)
-
Bjorn Stabell -
Evan Simpson