What is the difference between a template in ZCML and a ViewPageTemplateFile

By creating a BrowserView in Plone, I know that I can optionally customize the template with ZCML as follows:

<configure xmlns:browser="http://namespaces.zope.org/browser" > <browser:pageclass=".foo.FooView" template="foo.pt" … /> </configure> 

Or, alternatively, in code:

 # foo.py from Products.Five.browser.pagetemplatefile import ViewPageTemplateFile from zope.publisher.browser import BrowserPage class FooView(BrowserPage): """ My View """ def __call__(self): return ViewPageTemplateFile('foo.pt')(self) 

Is there a difference between the two approaches? Both seem to give the same result.

Subquery I know that there is a BrowserView class that can be imported, but usually everyone uses BrowserPage . What if there are significant differences between the two classes?

+6
source share
3 answers

Note. To fully comply with ZCML, you must set the index variable to indicate which template you are using. So TTW tuning will work too.

 # foo.py from Products.Five.browser.pagetemplatefile import ViewPageTemplateFile from zope.publisher.browser import BrowserPage class FooView(BrowserPage): index = ViewPageTemplateFile('foo.pt') 

Another template that you can use with a browser view is to add an update method.

 class FooView(BrowserPage): index = ViewPageTemplateFile('foo.pt') def __call__(self): self.update() return self.index() def update(self): self.portal_catalog = ... # initialize code 

But that is not a question.


So what is the difference? There is no difference . Browser browsing must be callable. The ZCML directive creates this so that the object has an index that should return the displayed page.

But creating a template for each call (your example) has one difference: you create a new instance of the template for each browser call. This does not apply to the class variable.

Last parameter: you do not need the class argument in the directive

 <configure xmlns:browser="http://namespaces.zope.org/browser"> <browser:pagetemplate="foo.pt" … /> </configure> 

For more information, you should read the directive code that uses SimpleViewClass, where src is the name of the template .

+7
source

In Plone, you can configure the TTW template (via portal_view_customizations ) only when the template is explicitly registered (for example, using ZCML or Grok directives).

If you define the template only in __call__ , you will not see it in portal_view_customizations .

In addition, I assume that loading the template inside the method will reload it from disk for each instance of the view (each request).

+7
source

AFAIK, no difference . The ZCML directive generates a ViewClass with a ViewPageTemplateFile and maps the template to __call__ . See Lines zope.browserpage.metaconfigure.page 132, 151.

This is exactly the same as in your example: you explicitly create a template in your __call__ method.

As for the subquery: in my opinion, significant differences do not appear in the context of Zope2 / Plone. Based on the interface ( zope.publisher.interfaces.browser.IBrowserPage ), BrowserPage is the base class you want to inherit from, since it implements __call__ and browserDefault . However, it doesn't matter if you use BrowserPage or BrowserView with Plone.

+1
source

All Articles