跳转到主要内容

ZPT宏的更简单定义。

项目描述

Latest release Supported Python versions https://github.com/zopefoundation/z3c.macro/actions/workflows/tests.yml/badge.svg https://coveralls.io/repos/github/zopefoundation/z3c.macro/badge.svg?branch=master

此包提供了一个适配器和TALES表达式,用于使用宏适配器注册表进行更明确和更灵活的宏处理。

详细文档

此包提供了一个适配器和TALES表达式,用于更明确和更灵活的宏处理,使用宏适配器注册表。

我们从创建一个内容对象开始,该对象将作为后续视图上下文使用

>>> import zope.interface
>>> import zope.component
>>> from zope.publisher.interfaces.browser import IBrowserView
>>> from zope.publisher.interfaces.browser import IDefaultBrowserLayer
>>> @zope.interface.implementer(zope.interface.Interface)
... class Content(object):
...     pass
>>> content = Content()

我们还创建了一个用于测试的示例模板的临时目录,我们将在稍后定义

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

宏模板

我们将宏模板定义为提供IMacroTemplate的适配器

>>> path = os.path.join(temp_dir, 'navigation.pt')
>>> with open(path, 'w') as file:
...     _ = file.write('''
... <metal:block define-macro="navigation">
...   <div tal:content="title">---</div>
... </metal:block>
... ''')

让我们定义宏工厂

>>> from z3c.macro import interfaces
>>> from z3c.macro import zcml
>>> navigationMacro = zcml.MacroFactory(path, 'navigation', 'text/html')

并将它们注册为适配器

>>> zope.component.provideAdapter(
...     navigationMacro,
...     (zope.interface.Interface, IBrowserView, IDefaultBrowserLayer),
...     interfaces.IMacroTemplate,
...     name='navigation')

TALES宏表达式

宏表达式将查找宏的名称,调用提供IMacroTemplate的适配器,并使用它们或在宏表达式中定义的槽中填充。

让我们使用导航宏创建一个页面模板

>>> path = os.path.join(temp_dir, 'first.pt')
>>> with open(path, 'w') as file:
...     _ = file.write('''
... <html>
...   <body>
...     <h1>First Page</h1>
...     <div class="navi">
...       <tal:block define="title string:My Navigation">
...         <metal:block use-macro="macro:navigation" />
...       </tal:block>
...     </div>
...     <div class="content">
...       Content here
...     </div>
...   </body>
... </html>
... ''')

如您所见,我们使用了宏表达式来简单地查找名为navigation的宏,它将被插入并替换此处的HTML内容。

现在让我们使用此页面模板创建一个视图

>>> from zope.publisher.browser import BrowserView
>>> class simple(BrowserView):
...     def __getitem__(self, name):
...         return self.index.macros[name]
...
...     def __call__(self, **kwargs):
...         return self.index(**kwargs)
>>> from zope.browserpage.viewpagetemplatefile import ViewPageTemplateFile
>>> def SimpleViewClass(path, name=u''):
...     return type(
...         "SimpleViewClass", (simple,),
...         {'index': ViewPageTemplateFile(path), '__name__': name})
>>> FirstPage = SimpleViewClass(path, name='first.html')
>>> zope.component.provideAdapter(
...     FirstPage,
...     (zope.interface.Interface, IDefaultBrowserLayer),
...     zope.interface.Interface,
...     name='first.html')

最后,我们查找视图并渲染它

>>> from zope.publisher.browser import TestRequest
>>> request = TestRequest()
>>> view = zope.component.getMultiAdapter((content, request),
...                                       name='first.html')
>>> print(view().strip())
<html>
  <body>
    <h1>First Page</h1>
    <div class="navi">
      <div>My Navigation</div>
    </div>
    <div class="content">
      Content here
    </div>
  </body>
</html>

插槽

我们还可以定义一个宏插槽,并用给定内容填充它

>>> path = os.path.join(temp_dir, 'addons.pt')
>>> with open(path, 'w') as file:
...     _ = file.write('''
... <metal:block define-macro="addons">
...   Content before header
...   <metal:block define-slot="header">
...     <div>My Header</div>
...   </metal:block>
...   Content after header
... </metal:block>
... ''')

让我们定义宏工厂

>>> addonsMacro = zcml.MacroFactory(path, 'addons', 'text/html')

并将它们注册为适配器

>>> zope.component.provideAdapter(
...     addonsMacro,
...     (zope.interface.Interface, IBrowserView, IDefaultBrowserLayer),
...     interfaces.IMacroTemplate,
...     name='addons')

让我们使用 addons 宏创建一个页面模板

>>> path = os.path.join(temp_dir, 'second.pt')
>>> with open(path, 'w') as file:
...     _ = file.write('''
... <html>
...   <body>
...     <h1>Second Page</h1>
...     <div class="header">
...       <metal:block use-macro="macro:addons">
...         This line get ignored
...         <metal:block fill-slot="header">
...           Header comes from here
...         </metal:block>
...         This line get ignored
...       </metal:block>
...     </div>
...   </body>
... </html>
... ''')

现在让我们使用此页面模板创建一个视图

>>> SecondPage = SimpleViewClass(path, name='second.html')
>>> zope.component.provideAdapter(
...     SecondPage,
...     (zope.interface.Interface, IDefaultBrowserLayer),
...     zope.interface.Interface,
...     name='second.html')

最后,我们查找视图并渲染它

>>> view = zope.component.getMultiAdapter((content, request),
...                                       name='second.html')
>>> print(view().strip())
<html>
  <body>
    <h1>Second Page</h1>
    <div class="header">
<BLANKLINE>
  Content before header
<BLANKLINE>
          Header comes from here
<BLANKLINE>
  Content after header
    </div>
  </body>
</html>

清理

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

宏指令

宏指令可以用来注册宏。查看 README.txt,它解释了 TALES 表达式宏。

>>> import sys
>>> from zope.configuration import xmlconfig
>>> import z3c.template
>>> context = xmlconfig.file('meta.zcml', z3c.macro)

首先定义一个包含宏的模板

>>> import os, tempfile
>>> temp_dir = tempfile.mkdtemp()
>>> file_path = os.path.join(temp_dir, 'file.pt')
>>> with open(file_path, 'w') as file:
...     _ = file.write('''
... <html>
...   <head>
...     <metal:block define-macro="title">
...        <title>Pagelet skin</title>
...     </metal:block>
...   </head>
...   <body>
...     <div>content</div>
...   </body>
... </html>
... ''')

并在 z3c:macroProvider 指令中注册宏提供者

>>> context = xmlconfig.string("""
... <configure
...     xmlns:z3c="http://namespaces.zope.org/z3c">
...   <z3c:macro
...       template="%s"
...       name="title"
...       />
... </configure>
... """ % file_path, context=context)

我们需要一个内容对象...

>>> import zope.interface
>>> @zope.interface.implementer(zope.interface.Interface)
... class Content(object):
...     pass
>>> content = Content()

我们需要一个视图...

>>> import zope.interface
>>> import zope.component
>>> from zope.publisher.browser import BrowserPage
>>> class View(BrowserPage):
...     def __init__(self, context, request):
...         self.context = context
...         self.request = request
以及一个请求
>>> from zope.publisher.browser import TestRequest
>>> request = TestRequest()

检查我们是否得到了宏模板

>>> from z3c.macro import interfaces
>>> view = View(content, request)
>>> macro = zope.component.queryMultiAdapter((content, view, request),
...     interface=interfaces.IMacroTemplate, name='title')
>>> macro is not None
True
>>> import os, tempfile
>>> temp_dir = tempfile.mkdtemp()
>>> test_path = os.path.join(temp_dir, 'test.pt')
>>> with open(test_path, 'w') as file:
...     _ = file.write('''
... <html>
...   <body>
...     <metal:macro use-macro="options/macro" />
...   </body>
... </html>
... ''')
>>> from zope.browserpage.viewpagetemplatefile import BoundPageTemplate
>>> from zope.browserpage.viewpagetemplatefile import ViewPageTemplateFile
>>> template = ViewPageTemplateFile(test_path)
>>> print(BoundPageTemplate(template, view)(macro=macro))
<html>
  <body>
    <title>Pagelet skin</title>
  </body>
</html>

错误条件

如果文件不可用,则指令失败

>>> context = xmlconfig.string("""
... <configure
...     xmlns:z3c="http://namespaces.zope.org/z3c">
...   <z3c:macro
...       template="this_file_does_not_exist"
...       name="title"
...       />
... </configure>
... """, context=context)
Traceback (most recent call last):
...
zope.configuration.exceptions.ConfigurationError: ...

更改

3.0 (2024-01-11)

  • 添加对 Python 3.11、3.12 的支持。

  • 移除对 Python 2.7、3.5、3.6 的支持。

2.3 (2021-12-16)

  • 添加对 Python 3.5、3.8、3.9 和 3.10 的支持。

2.2.1 (2018-12-05)

  • 在 Trove 类别中修复支持的 Python 版本列表:目前支持的 Python 版本是 2.7、3.6、3.7、PyPy2 和 PyPy3。

  • 使用 Flake8 检查代码。

2.2.0 (2018-11-13)

  • 移除了 Python 3.5 的支持,添加了 Python 3.7。

  • 修复了测试。

  • 修复了导致 DeprecationWarning 的文档字符串。

2.1.0 (2017-10-17)

  • 移除了对 Python 2.6 和 3.3 的支持。

  • 添加了对 Python 3.4、3.5 和 3.6 的支持。

  • 添加了对 PyPy 的支持。

2.0.0 (2015-11-09)

  • 标准化 __init__ 命名空间。

2.0.0a1 (2013-02-25)

  • 添加了对 Python 3.3 的支持。

  • 用等效的 zope.interface.implementer 装饰器替换了已弃用的 zope.interface.implements 使用。

  • 移除了对 Python 2.4 和 2.5 的支持。

1.4.2 (2012-02-15)

  • 由于 z3c.pt 可用但 z3c.ptcompat 未包含,因此移除了使用 ViewPageTemplateFile 的钩子。如 1.4.0 版本中的注释所建议。

1.4.1 (2011-11-15)

  • bugfix,安装要求列表中缺少逗号。

1.4.0 (2011-10-29)

  • 将 z3c.pt 包含移动到 extras_require chameleon 中。这使得该包与 chameleon 和其他包独立,并允许将此依赖项包含在您的项目中。

  • 升级到 chameleon 2.0 模板引擎,并使用最新的 z3c.pt 和 z3c.ptcompat 软件包,这些软件包已调整以与 chameleon 2.0 兼容。

    查看 z3c.ptcompat 软件包中的注释。

    更新 z3c.ptcompat 实现以使用基于组件的模板引擎配置,直接插入 Zope 工具包框架。

    z3c.ptcompat 软件包不再提供模板类,或 ZCML 指令;您应直接从 ZTK 代码库导入。

    注意,PREFER_Z3C_PT 环境选项已被弃用;现在,这是通过组件配置管理的。

    此外,请注意,chameleon 的 CHAMELEON_CACHE 环境值已从 True/False 更改为路径。如果您不喜欢使用缓存,请跳过此属性。在 buildout 环境部分中定义的 None 或 False 不起作用。至少在 chameleon <= 2.5.4 中如此。

    注意:您需要包含 z3c.ptcompat 中的 configure.zcml 文件以启用 z3c.pt 模板引擎。configure.zcml 将插件模板引擎。此外,请删除任何自定义构建的钩子,这些钩子将在您的测试或其他位置导入 z3c.ptcompat。

1.3.0 (2010-07-05)

  • 测试现在需要 zope.browserpage >= 3.12 而不是 zope.app.pagetemplate 作为表达式类型注册,因为最近已将其移动到那里。

  • 不再使用已弃用的 zope.testing.doctestunit,而是使用内置的 doctest

1.2.1 (2009-03-07)

  • 仅存在 z3c.pt 不足以注册宏实用程序,还需要 chameleon.zpt,否则实用程序的工厂未定义。

1.2.0 (2009-03-07)

  • 允许使用 z3c.pt 通过 z3c.ptcompat 兼容层。

  • 将软件包的邮件列表地址更改为 zope-dev at zope.org。

1.1.0 (2007-11-01)

  • 更新软件包信息数据。

  • 添加 z3c 命名空间软件包声明。

1.0.0 (2007-09-30)

  • 初始发布。

项目详细信息


下载文件

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

源分布

z3c.macro-3.0.tar.gz (16.8 kB 查看哈希值)

上传时间

构建分布

z3c.macro-3.0-py3-none-any.whl (16.1 kB 查看哈希值)

上传时间 Python 3

支持者

AWSAWS云计算和安全赞助商DatadogDatadog监控FastlyFastlyCDNGoogleGoogle下载分析MicrosoftMicrosoftPSF赞助商PingdomPingdom监控SentrySentry错误日志StatusPageStatusPage状态页面