[Grok-dev] Traversing over attributes/methods in grok.View

Jasper Spaans j at jasper.es
Mon Sep 13 08:31:52 EDT 2010


On Mon, Sep 13, 2010 at 01:58:23PM +0200, Christian Klinger wrote:

> i try to access attributes/methods of a grok.View via traversal.
> But i have no idea where to look for a solution.
> 
> Take a look at this exmaple: I think it shows what i want...
> 
> class MyView(grok.View):
> 
>      def foo(self):
>          return "foo"
> 
>      def render(self):
>          return "normal view"
> 
>  >>> browser.open('http://localhost/app/myview')
>  >>> browser.contents
> "normal view"
> 
>  >>> browser.open('http://localhost/app/myview/foo')
>  >>> browser.contents
> "foo"
> 
> 
> Any idea how i can do this? I guess i have to look in zope.publication 
> and or zope.traversal.

The closest you can get probably is grok.traversable:
http://grok.zope.org/doc/current/reference/directives.html#grok.traversable
but than will need you to have methods which are defined on your Model
class instead of on your view.

You can than add an adapter for string-like objects, which returns the
things as-is, like

class MyStringViewer(grok.View)
    grok.context(basestring)

    def render(self):
        return self.context

[*] completely untested code here, but it might work.

Cheers,
Jasper
-- 
Ir. Jasper Spaans                                   http://jasper.es/
 14:28:23 up 12262 days,  5:15, 1 user, load average: 0.00 0.00 0.00

           emacs... car rater vi, c'est un droit inaliénable


More information about the Grok-dev mailing list