[Zope] Help! How do I get my namespace in Python??

Jim Fulton jim@digicool.com
Tue, 14 Dec 1999 22:53:47 +0000


Andrew Wilcox wrote:
> 
> >> But the DTML Method doesn't have access to the namespace!
> >
> >What namespace?
> 
> The regular DTML namespace, _.
> 
> >You probably want the instance's namespace plus the request
> >namespace.  If this is the case, you want:
> >
> >     def test(self, REQUEST=None):
> >         return self.myDTMLMethod(self, REQUEST)
> >
> 
> Nope, doesn't work!
> 
> For example, first try a plain DTML Document called "A":
> 
>     <dtml-var standard_html_header>
>     <dtml-let food="'Popcorn'">
>       X says: <dtml-var X>
>     </dtml-let>
>     <dtml-var standard_html_footer>
> 
> which calls a DTML Method called "X":
> 
>     My favorite food is <dtml-var food>
> 
> Run A and you'll get:
> 
>     X says: My favorite food is Popcorn
> 
> Now make a method called "test" which also calls X:
> 
>     def test(self, REQUEST=None):
>         return self.X(self, REQUEST)
> 
> But try to call X through test:
> 
>     <dtml-var standard_html_header>
>     <dtml-let food="'Popcorn'">
>      test says: <dtml-var test>
>     </dtml-let>
>     <dtml-var standard_html_footer>
> 
> and I get:
> 
>     Error Type: KeyError
>     Error Value: food
> 
> See, X can no longer see "food" in the namespace.

If this is what you want to do, then you need to pass the namespace 
to the Python function. You will have to do this explicitly.


     def test(self, namespace):
         return self.myDTMLMethod(self, namespace)

if test is an ordinary Python method (defined
in some base class), then you don't need to 
pass self, so from DTML:

     <dtml-var standard_html_header>
     <dtml-let food="'Popcorn'">
      test says: <dtml-var expr="test(_)">
     </dtml-let>
     <dtml-var standard_html_footer>

If test is an external method, then, unfortunately,
you need to pass the self along:

     <dtml-var standard_html_header>
     <dtml-let food="'Popcorn'">
      test says: <dtml-var expr="test(this(),_)">
     </dtml-let>
     <dtml-var standard_html_footer>

Jim

--
Jim Fulton           mailto:jim@digicool.com
Technical Director   (888) 344-4332              Python Powered!
Digital Creations    http://www.digicool.com     http://www.python.org

Under US Code Title 47, Sec.227(b)(1)(C), Sec.227(a)(2)(B) This email
address may not be added to any commercial mail list with out my
permission.  Violation of my privacy with advertising or SPAM will
result in a suit for a MINIMUM of $500 damages/incident, $1500 for
repeats.