2012-12-14 21 views
6

當創建在Plone一個BrowserView模板之間的區別,我知道我可以選擇配置模板,ZCML像這樣:是什麼在ZCML和ViewPageTemplateFile

<configure 

    xmlns:browser="http://namespaces.zope.org/browser" 
    > 

    <browser:page 
     … 
     class=".foo.FooView" 
     template="foo.pt" 
     … 
     /> 

</configure> 

或可替代代碼:

# 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) 

這兩種方法有什麼不同嗎?他們都似乎產生相同的結果。

子問題:我知道有一個BrowserView類一個可以導入,但是通常每個人都使用BrowserPage。如果兩個類別之間存在顯着差異怎麼辦?

回答

7

注意:要完全等價於ZCML,您應該設置index變量以指定您正在使用的模板。這樣的TTW定製也將工作。

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

您可以在瀏覽器視圖中使用的另一種模式是添加更新方法。

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

    def update(self): 
     self.portal_catalog = ... # initialize code 

但這不是問題。


那麼有什麼區別? 沒有區別。瀏覽器視圖必須是可調用的。 ZCML指令以對象具有必須返回呈現的頁面的索引的方式構建該可調用對象。

但是在每次調用時創建模板(您的示例)有一個區別: 您正在瀏覽器視圖的每次調用中創建模板的新實例。 這不是類變量的情況。

最後一個選項:你並不需要一個類參數的指令

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

欲瞭解更多信息,請閱讀the code of the directive,它採用SimpleViewClass where src is the template name

+0

請不要在這裏的帖子中使用URL更短的內容,絕對沒有必要也不需要模糊你正在鏈接的內容。 –

+0

嗨,但願svn.zope.org網址在哪裏不被識別爲*,因爲它的URL。感謝編輯 – toutpt

+0

正確的,當用作普通URL(而不是鏈接)時,配對的'*'星號被解釋爲'斜體'的標記。 –

1

AFAIK,有沒有區別。 ZCML指令生成一個帶有ViewPageTemplateFile的ViewClass,並在__call__上呈現模板。請參見zope.browserpage.metaconfigure.page第132,151行。

這與您在示例中完全相同:您在__call__方法中明確實例化模板。

關於子問題:根據我的理解,在Zope2/Plone的背景下,顯着差異並不明顯。基於接口(zope.publisher.interfaces.browser.IBrowserPage),BrowserPage是您想要繼承的基類,因爲它實現了__call__browserDefault。然而,如果你在Plone上使用BrowserPageBrowserView,這似乎並不重要。

7

在Plone中,只有在顯式註冊模板(例如使用ZCML或Grok指令)時,纔可以自定義模板TTW(通過portal_view_customizations)。

如果您只在您的__call__中定義模板,您將不會在portal_view_customizations中看到它。

另外,我猜想在一個方法中加載模板會從磁盤爲每個視圖實例(每個請求)重新加載它。

+0

哦,只是跳到@ toutpt的答案。我不知道TTW自定義所需的魔術只是將定義模板放置爲「索引」類變量。 –