gpt4 book ai didi

python - ZCML 中的模板和 ViewPageTemplateFile 有什么区别

转载 作者:太空狗 更新时间:2023-10-29 22:08:13 27 4
gpt4 key购买 nike

创建 BrowserView 时在 Plone 中,我知道我可以选择使用 ZCML 配置模板,如下所示:

<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。如果两个类之间存在任何显着差异怎么办?

最佳答案

注意:要完全等同于 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')

您可以在浏览器 View 中使用的另一种模式是添加更新方法。

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

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

但这不是问题。


那么有什么区别呢? 没有区别。浏览器 View 必须是可调用的。 ZCML 指令以对象具有索引的方式构建此可调用对象,该索引必须返回呈现的页面。

但是在每次调用时创建模板(你的例子)有一个区别:您在浏览器 View 的每次调用中都创建了一个新的模板实例。类变量不是这种情况。

最后一个选项:您不需要在指令中使用类参数

<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 .

关于python - ZCML 中的模板和 ViewPageTemplateFile 有什么区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13871915/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com