0

I have a folderish ATDocument class create through generic setup, generic setup xml configuration file has the following view variables defined:

<property name="immediate_view">TemplateFileName</property>
<property name="default_view">TemplateFileName_view</property>
<property name="view_methods">
    <element value="TemplateFileName_view"/>
</property>

Is it possible to replace that view with a dispatcher, i.e. a BrowserView class that on __call__ would dispatch an actual ViewPagetTemplateFile() instance?

I tried replacing template file name with a method name of that class but that doesn't seem to work. Also I followed inheritance tree of ATDocument though ATCTContent to BaseContent, but I didn't find any definitions of views, so I'm guessing views are processed via one of the inherited mix-ins.

Alex Volkov
  • 2,812
  • 23
  • 27

1 Answers1

1

The names in the GenericSetup xml file are either view names or skin items; view names are looked up using the same traversal mechanisms as when you direcly name the view in the URL.

So, anything you can reach with a URL can be used as a view method. That includes views that dispatch to other views in the __call__ method:

from zope.publisher.browser import BrowserView
from zope.component import getMultiAdapter

class DispatchingView(BrowserView):
    def __call__(self):
        if somecondition:
            newviewname = 'foo'
        else:
            newviewname = 'bar'

        return getMultiAdapter((self.context, self.request), name=newviewname)()

This example view looks up other views by their name, and renders them in place to return as the result of the dispatcher itself.

Note that generally, I make sure that if a view is being used I make sure to include the @@ view namespace in front of it's name to prevent that a skin item with the same name accidentally is being used.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • Thank worked beautifully, thanks. I'm not sure about '@@' appended to the beginning of the name, I should declare a view without '@@' and only use '@@' in a link so zope would skip traversing through skins? – Alex Volkov Jun 08 '12 at 14:46
  • Exactly. The "goggles" (double @) is actually shorthand for "++view++", which is a URL namespace qualifier. Different namespaces can be used for different purposes, but the "@@" view is the best-known. It helps the publisher find the right items even when names conflict. – Martijn Pieters Jun 08 '12 at 15:05