pagelets是不使用o-wrap指定模板的方法。
z3c.pagelet的Python项目详细描述
小页
这个包提供了一个非常灵活的基本实现,可以使用 编写视图组件,这些组件稍后可以在自定义项目中高度自定义。 如果必须编写可重用的组件 在一个框架中。Pagelet是不同的浏览器页面,可以使用 替换它们。
这是什么意思?
我们将python视图代码与模板实现分离。我们也 在至少两个不同的模板中分离模板-内容 模板和布局模板。
这个包使用z3c.template并为此提供了一个实现 模板模式。另外,这个包还提供了一个 pagelet 指令 wich可用于注册pagelet。
pagelet是可以调用并支持更新和呈现的视图 模式:
如何工作
pagelet返回呈现方法中没有布局的呈现内容,并且 如果调用布局代码,则返回它。另请参见z3c.template,其中显示 模板的工作原理。这些示例将只显示基本实现 位于z3c.pagelet.browser模块中,请使用。
BrowserPagelet
名为BrowserPagelet的基本实现提供内置调用和 提供不同模板查找的呈现方法。看看 BrowserPagelet类位于z3c.pagelet.browser中,您可以看到 方法返回一个IContentTemplate和一个IlayoutTemplate 在z3c.layout包中定义。
< Buff行情>>>> import os, tempfile >>> temp_dir = tempfile.mkdtemp()
>>> import zope.interface >>> import zope.component >>> from z3c.pagelet import interfaces >>> from z3c.pagelet import browser
我们首先定义一个呈现Pagelet内容的页面模板。
< Buff行情>>>> contentTemplate = os.path.join(temp_dir, 'contentTemplate.pt') >>> with open(contentTemplate, 'w') as file: ... _ = file.write(''' ... <div class="content"> ... my template content ... </div> ... ''')< >我们还定义了一个布局模板来呈现pagelet的布局。 此模板将从pagelet调用render方法: < Buff行情>
>>> layoutTemplate = os.path.join(temp_dir, 'layoutTemplate.pt') >>> with open(layoutTemplate, 'w') as file: ... _ = file.write(''' ... <html> ... <body> ... <div class="layout" tal:content="structure view/render"> ... here comes the content ... </div> ... </body> ... </html> ... ''')
现在让我们为视图和请求注册模板。我们使用 直接来自z3c.template包的templatefactory。这是常见的做法 使用名为 z3c:template 的zcml指令。请注意,我们确实使用 泛型接口作为视图基接口来注册模板。这个 允许我们在下一个示例中注册更具体的模板:
< Buff行情>>>> from zope.publisher.interfaces.browser import IDefaultBrowserLayer >>> from z3c.template.interfaces import IContentTemplate >>> from z3c.template.template import TemplateFactory >>> factory = TemplateFactory(contentTemplate, 'text/html') >>> zope.component.provideAdapter( ... factory, (zope.interface.Interface, IDefaultBrowserLayer), ... IContentTemplate)
并使用 接口将布局模板注册为注册基:
< Buff行情>>>> from z3c.template.interfaces import ILayoutTemplate >>> factory = TemplateFactory(layoutTemplate, 'text/html') >>> zope.component.provideAdapter(factory, ... (zope.interface.Interface, IDefaultBrowserLayer), ILayoutTemplate)
现在定义一个视图标记接口。这样的标记接口用于 我们注册模板:
< Buff行情>>>> class IMyView(zope.interface.Interface): ... pass
我们定义了一个从BrowserPagelet继承的视图类,并实现了 查看标记界面:
< Buff行情>>>> @zope.interface.implementer(IMyView) ... class MyView(browser.BrowserPagelet): ... pass
现在测试提供视图的view类并检查输出:
< Buff行情>>>> from zope.publisher.browser import TestRequest >>> request = TestRequest() >>> myView = MyView(root, request) >>> print(myView()) <html> <body> <div class="layout"> <div class="content"> my template content </div> </div> </body> </html>
您可以看到render方法只生成内容:
< Buff行情>>>> import os, tempfile >>> temp_dir = tempfile.mkdtemp()0
重定向
当请求是 重定向,因为渲染对中的浏览器请求没有任何意义 那个案子。让我们创建一个在其更新方法中进行重定向的视图。
< Buff行情>>>> import os, tempfile >>> temp_dir = tempfile.mkdtemp()1
当作为浏览器页调用时,它将返回一个空字符串。
< Buff行情>>>> import os, tempfile >>> temp_dir = tempfile.mkdtemp()2
但是, render 方法将一如既往地呈现pagelet的模板:
< Buff行情>>>> import os, tempfile >>> temp_dir = tempfile.mkdtemp()3
页面渲染器
还有一个标准模式用于在pagelet上调用render方法。 使用作为icontentprovider的pagelet呈现程序可以 不使用pagelet重用现有布局模板。如果你想重复使用 没有页面的布局模板让您只需提供另一个内容 供应商.它很灵活,不是吗?接下来,让我们使用 Pagelet渲染器。
我们使用名为pagelet的内容提供程序定义了一个新的布局模板 < Buff行情>
>>> import os, tempfile >>> temp_dir = tempfile.mkdtemp()4
然后登记。现在我们使用视图中定义的特定接口:
< Buff行情>>>> import os, tempfile >>> temp_dir = tempfile.mkdtemp()5
现在我们调用视图:
< Buff行情>>>> import os, tempfile >>> temp_dir = tempfile.mkdtemp()6
对,我们需要先注册内容提供者 可以使用。
< Buff行情>>>> import os, tempfile >>> temp_dir = tempfile.mkdtemp()7
现在我们再次调用视图:
< Buff行情>>>> import os, tempfile >>> temp_dir = tempfile.mkdtemp()8
上下文特定模板
pagelet还可以使用上下文对象查找模板 作为附加的鉴别器,via(self,self.request,self.context) 查找。当您希望为 一些特定的内容对象。让我们检查一下。
首先,让我们定义一个自定义内容类型并创建一个要使用的对象:
< Buff行情>>>> import os, tempfile >>> temp_dir = tempfile.mkdtemp()9
>>> import zope.interface >>> import zope.component >>> from z3c.pagelet import interfaces >>> from z3c.pagelet import browser0
让我们使用前面定义的视图类。目前,它将使用 我们之前定义的布局和内容模板(视图、请求):
< Buff行情>>>> import zope.interface >>> import zope.component >>> from z3c.pagelet import interfaces >>> from z3c.pagelet import browser1
让我们创建特定于上下文的布局和内容模板并注册 它们用于我们的icontent界面:
< Buff行情>>>> import zope.interface >>> import zope.component >>> from z3c.pagelet import interfaces >>> from z3c.pagelet import browser2
>>> import zope.interface >>> import zope.component >>> from z3c.pagelet import interfaces >>> from z3c.pagelet import browser3
现在,我们的视图应该使用特定于上下文的模板进行渲染:
< Buff行情>>>> import zope.interface >>> import zope.component >>> from z3c.pagelet import interfaces >>> from z3c.pagelet import browser4
添加、编辑和显示表单(formlib)
如果没有任何基于formlib的实现,pagelet会是什么? 我们提供基本实施方案用于添加、编辑和显示基于 表单库。 注意: 要确保定义了这些类,请 应该已经安装了zope.formlib 不直接依赖于zope.formlib 表单库。
在接下来的测试中,我们提供一个通用的表单模板 就像在formlib中使用的那样。此模板已在此包中注册 作为基于formlib的mixin类的默认值:
< Buff行情>>>> import zope.interface >>> import zope.component >>> from z3c.pagelet import interfaces >>> from z3c.pagelet import browser5
我们定义了一个包含文本属性的新接口:
< Buff行情>>>> import zope.interface >>> import zope.component >>> from z3c.pagelet import interfaces >>> from z3c.pagelet import browser6
同时定义一个实现接口的内容对象:
< Buff行情>>>> import zope.interface >>> import zope.component >>> from z3c.pagelet import interfaces >>> from z3c.pagelet import browser7
页面地址表单
现在,让我们基于pageletaddform类定义一个add from:
< Buff行情>>>> import zope.interface >>> import zope.component >>> from z3c.pagelet import interfaces >>> from z3c.pagelet import browser8
现在呈现表单:
< Buff行情>>>> import zope.interface >>> import zope.component >>> from z3c.pagelet import interfaces >>> from z3c.pagelet import browser9
页面删除编辑表单
现在,让我们基于pageleteditform类定义一个编辑表单:
< Buff行情>>>> contentTemplate = os.path.join(temp_dir, 'contentTemplate.pt') >>> with open(contentTemplate, 'w') as file: ... _ = file.write(''' ... <div class="content"> ... my template content ... </div> ... ''')0
并呈现表单:
< Buff行情>>>> contentTemplate = os.path.join(temp_dir, 'contentTemplate.pt') >>> with open(contentTemplate, 'w') as file: ... _ = file.write(''' ... <div class="content"> ... my template content ... </div> ... ''')1
页面显示窗体
现在,让我们根据PageLetDisplayForm类定义一个显示窗体…
< Buff行情>>>> contentTemplate = os.path.join(temp_dir, 'contentTemplate.pt') >>> with open(contentTemplate, 'w') as file: ... _ = file.write(''' ... <div class="content"> ... my template content ... </div> ... ''')2
并呈现表单:
< Buff行情>>>> contentTemplate = os.path.join(temp_dir, 'contentTemplate.pt') >>> with open(contentTemplate, 'w') as file: ... _ = file.write(''' ... <div class="content"> ... my template content ... </div> ... ''')3
清理
< Buff行情>>>> contentTemplate = os.path.join(temp_dir, 'contentTemplate.pt') >>> with open(contentTemplate, 'w') as file: ... _ = file.write(''' ... <div class="content"> ... my template content ... </div> ... ''')4