[Zope] HTMLFile from within a method?

Terry Kerr terry@adroit.net
Fri, 14 Jul 2000 10:56:15 +1000


Rob Miller wrote:

> Greetings.  I'm a fairly experienced Python programmer, but am new to Zope.
> I'm working on a Python product, and through this process I'm learning my way
> around the Zope innards.  I'm running Zope 2.2.0b3 on an RH6.1 box.
>
> By declaring a class member that is an instance of the HTMLFile class, I
> can publish dtml files.  This is right out of the "Boring Product" How-To; it
> looks like this:
>
> ----------------------
> class Product(Implicit, Persistent, RoleManager, Folder):
>
>     index_html = HTMLFile('index', globals())
> ----------------------
>
> Then when I browse to an installed instance of my Product, I'll see the
> appropriate output generated by my 'index.dtml' file, which is in the same
> directory as my Product's python modules.
>
> What I'm having trouble doing is something similar but from within one of my
> Product class's methods.  I've tried this:
>
> ----------------------
> class Product(Implicit, Persistent, RoleManager, Folder):
>
>     def form_handler(self, REQUEST=None):
>         return HTMLFile('form_results', globals())
> ----------------------
>
> This causes the output to be munged, like so:
>
> <dtml-var standard_html_header>
> <dtml-in "REQUEST.keys()">
> ... and so on ...
>
> I've also tried:
>
> ----------------------
> class Product(Implicit, Persistent, RoleManager, Folder):
>
>     form_results = HTMLFile('form_results', globals())
>
>     def form_handler(self, REQUEST=None):
>         return self.form_results()
> ----------------------

This should work u change the last line to

    return self.form_results(self,REQUEST).

You need to explicity pass the namespace to it, ie 'self'.  You may or may not
need to pass REQUEST as well.


>
>
> This causes zope to attempt to publish the object, but the object (my DTML
> method, stored in "form_results.dtml") doesn't seem to have access to the
> Product's namespace.  That is, I get an error like:
>
> Error Type: KeyError
> Error Value: standard_html_header
>
> ...even though there's definitely a standard_html_header defined.
>
> Finally, I've tried this:
>
> ----------------------
> class Product(Implicit, Persistent, RoleManager, Folder):
>
>     def form_handler(self, REQUEST=None):
>         dtml = open('lib/python/Products/Product/form_results.dtml', 'r')
>         s = dtml.read()
>         dtml.close()
>         return s
> -----------------------
>
> This causes the right data to be passed out, but it's not treated
> as dtml.  A garbled version of my output appears in the browser window, and if
> I view source I see:
>
> <html><head></head>
> <dtml-var standard_html_header>
> <dtml-in "REQUEST.keys()">
> ... and so on ...
>
> I know there are other ways to accomplish what I'm trying to accomplish, but I
> really want to understand what's going on here.  Can anyone enlighten me as to
> why I'm seeing the results I'm seeing?  Does anyone have suggestions for an
> appropriately Zen approach to publishing dtml files from within a Product's
> methods?
>
> Thanks for your time,
>
> rob
>
> _______________________________________________
> 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 )

--
Terry Kerr (terry@adroit.net)
Adroit Internet Solutions Pty Ltd (www.adroit.net)
Phone:   +613 9563 4461
Fax:     +613 9563 3856
Mobile:  +61 414 938 124
ICQ:     79303381