跳转到主要内容

基于ZPT宏的视图组件。

项目描述

宏视图组件是Zope 3 UI组件。特别是它们允许开发人员指定基于宏的视图组件,而不是整个模板。

宏提供者

此软件包提供了一个ZCML指令,允许您将模板中定义的宏注册为视图组件。此类基于宏的视图组件与其他视图组件完全相同。如果您想在单个页面模板中编写布局模板,并将选择性部分定义为视图组件而不添加任何额外的HTML,这将非常有用。让我展示一下它将是什么样子。

布局/主模板可以如下所示

<!DOCTYPE ...>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"
      i18n:domain="z3c">
<head>
<tal:block replace="structure provider:ITitle">
  <metal:block define-macro="title">
    <title>The title</title>
  </metal:block>
</tal:block>
</head>
<body tal:define="applicationURL request/getApplicationURL">
<div id="content">
  <tal:block content="structure provider:pagelet">content</tal:block>
</div>
</div>
</body>
</html>

上面的模板定义了一个ITitle提供者,它包含宏的定义。您必须在zope.viewlet ZCMl指令中定义一个视图组件管理器,它提供ITitle作为视图组件管理器。之后,您可以在z3c:layout ZCML指令中注册上面的模板作为布局模板,如下所示

<z3c:layout
    for="*"
    layer="z3c.skin.pagelet.IPageletBrowserSkin"
    template="template.pt"
    />

然后您可以注册宏视图组件以供ITitle视图组件管理器使用,如下所示

<z3c:macroViewlet
    for="*"
    template="template.pt"
    macro="title"
    manager="z3c.skin.pagelet.ITitle"
    layer="z3c.skin.pagelet.IPageletBrowserSkin"
    />

如您所见,上面的ZCML配置指令使用title作为宏属性,并使用ITitle作为视图组件管理器。这将使用以下部分模板.pt

<title>Pagelet skin</title>

并将其注册为视图小部件。这个小部件在ITitle提供者中渲染。如您所见,您可以使用完整的布局模板,并且可以直接使用。接下来,您可以提供一个包含视图小部件管理器的视图小部件,该管理器可以用于其他上下文或视图等。您还可以为ITitle视图小部件管理器注册多个视图小部件。当然,在我们的特殊标题标签示例中,这没有任何意义。

让我们通过一些测试来展示这一点。我们将首先创建一个内容对象,稍后将其用作视图上下文。

>>> import zope.interface
>>> import zope.component
>>> from zope.publisher.interfaces.browser import IBrowserView
>>> from zope.publisher.interfaces.browser import IDefaultBrowserLayer
>>> class Content(object):
...     zope.interface.implements(zope.interface.Interface)

>>> content = Content()

我们还创建了一个用于样本模板的临时目录,稍后将定义它进行测试。

>>> import os, tempfile
>>> temp_dir = tempfile.mkdtemp()

然后我们为MacroViewlet类注册一个安全检查器。

>>> from zope.configuration.xmlconfig import XMLConfig
>>> import zope.app.component
>>> import z3c.macroviewlet
>>> XMLConfig('meta.zcml', zope.app.component)()
>>> XMLConfig('configure.zcml', z3c.macroviewlet)()

布局模板

我们定义一个包含宏定义并使用提供者的模板。

>>> path = os.path.join(temp_dir, 'template.pt')
>>> open(path, 'w').write('''
... <html>
... <body>
... <head>
... <tal:block replace="structure provider:ITitle">
...   <metal:block define-macro="title">
...     <title>The title</title>
...   </metal:block>
... </tal:block>
... </head>
... <body tal:define="applicationURL request/getApplicationURL">
...   content
... </body>
... </html>
... ''')

让我们使用视图模板注册一个视图类。

>>> import zope.interface
>>> from zope.app.pagetemplate import viewpagetemplatefile
>>> from zope.publisher.interfaces.browser import IBrowserView
>>> class View(object):
...     zope.interface.implements(IBrowserView)
...     def __init__(self, context, request):
...        self.context = context
...        self.request = request
...     def __call__(self):
...         return viewpagetemplatefile.ViewPageTemplateFile(path)(self)

让我们准备视图。

>>> from zope.publisher.browser import TestRequest
>>> request = TestRequest()
>>> view = View(content, request)

让我们定义视图小部件管理器 ITitle

>>> from zope.viewlet.interfaces import IViewletManager
>>> from zope.viewlet.manager import ViewletManager
>>> class ITitle(IViewletManager):
...     """Viewlet manager located in the title tag."""

>>> title = ViewletManager('title', ITitle)

让我们注册视图小部件管理器。

>>> from zope.viewlet.interfaces import IViewletManager
>>> manager = zope.component.provideAdapter(
...     title,
...     (zope.interface.Interface, TestRequest, IBrowserView),
...     IViewletManager,
...     name='ITitle')

MacroViewlet

在我们注册宏视图小部件之前,我们将检查没有注册任何宏视图小部件时渲染的页面。

>>> print view()
<html>
<body>
<head>
</head>
<body>
  content
</body>
</body></html>

如您所见,没有渲染标题。现在我们可以定义宏视图小部件...

>>> from zope.app.pagetemplate import viewpagetemplatefile
>>> from z3c.macroviewlet import zcml
>>> macroViewlet = zcml.MacroViewletFactory(path, 'title', 'text/html')

并将它们注册为适配器。

>>> from zope.viewlet.interfaces import IViewlet
>>> zope.component.provideAdapter(
...     macroViewlet,
...     (zope.interface.Interface, IDefaultBrowserLayer, IBrowserView,
...      ITitle),
...     IViewlet,
...     name='title')

现在我们准备再次进行测试。

>>> print view()
<html>
<body>
<head>
    <title>The title</title>
</head>
<body>
  content
</body>
</body></html>

如您所见,标题被渲染为小部件并嵌入到ITitle提供者中。

清理

>>> import shutil
>>> shutil.rmtree(temp_dir)

变更

1.1.0 (2010-07-14)

  • 修复了与当前包版本兼容的测试(metaconfigure.registerType 已从 zope.app.pagetemplate 移至 zope.browserpage)。因此现在需要 zope.browserpage

  • 不再使用过时的 zope.testing.doctestzope.testing.doctestunit

  • 移除了ZPKG和ZCML占位符。

  • 修复了README中的RST格式。

1.0.0 (2007-09-21)

  • 初始发布

项目详情


下载文件

下载适合您平台的文件。如果您不确定选择哪个,请了解更多关于 安装包 的信息。

源代码发行版

z3c.macroviewlet-1.1.0.tar.gz (8.6 kB 查看散列)

上传时间 源代码

支持者

AWS AWS 云计算和安全赞助商 Datadog Datadog 监控 Fastly Fastly CDN Google Google 下载分析 Microsoft Microsoft PSF 赞助商 Pingdom Pingdom 监控 Sentry Sentry 错误记录 StatusPage StatusPage 状态页面