plone - 在 Chameleon 中使用 tal 宏

标签 plone chameleon

我是 Plone 用户,我使用 Products.Five.browser.pagetemplatefile.ViewPageTemplateFile 中的 tal 宏已经有一段时间了,并创建了一个包含现有宏和模板的库。

我通过 grokcore.chameleon 1.0.3 开始使用变色龙页面模板,并希望在现有框架内继续使用它们。 IE。我希望能够导入 tal 宏,然后使用变色龙填充宏槽。

到目前为止,我尝试了几种导入现有宏的方法,但都没有奏效。即使安装了 Chameleon 2.14[1],也未启用“加载”关键字。

我一直在寻找兼容层,但目前我只找到了z3c.pt,其目的是加速.pt页面渲染并且不提供兼容层。[2]

是否有激活 tal 宏然后从 Chameleon 页面模板插入信息的软件包? 作为一种解决方法,我可以渲染 tal 模板,渲染变色龙模板,然后进行字符串替换,但必须有人以更优雅的方式解决了这个问题。

[1] how to use macros with pyramid / ZPT (Chameleon)

[2] https://pypi.python.org/pypi/z3c.pt

更新 作为解决方法,我创建了一个生成中间页面的函数,该页面接受变色龙页面模板生成的 html。

common.py

from zope.publisher.browser import BrowserView
from Products.Five.browser.pagetemplatefile import ViewPageTemplateFile

def insert_into_master(html, view):

    class View(BrowserView):
        def __call__(self):
            self.data = html
            return ViewPageTemplateFile('pt/master_holder.pt')(self)

    rendered = View(view.context, view.request)
    return rendered()

pt/master_holder.pt

<html
xmlns="http://www.w3.org/1999/xhtml"
xmlns:tal="http://xml.zope.org/namespaces/tal"
xmlns:metal="http://xml.zope.org/namespaces/metal"
xmlns:i18n="http://xml.zope.org/namespaces/i18n"
metal:use-macro="here/main_template/macros/master"
i18n:domain="Plone"
xml:lang="en"
lang="en">

<div
    metal:fill-slot="main"
    tal:content="structure:view/data"
/>

</html>

任何使用变色龙的客户端 View

from five import grok
from zope.interface import Interface
from grokcore.chameleon.components import ChameleonPageTemplate

from common import insert_into_master

class MyView(grok.View):
    grok.context(Interface)
    grok.require('zope2.View')
    grok.name('myview')

    def render(self):
        view = ChameleonPageTemplate('<div>Hello, world!</div>')
        return insert_into_master(view.render(self), self)

最佳答案

Chameleon 在内部以不同于 Zope 的标准页面模板实现的形式表示宏,并且两者不兼容。因此,您只能使用来自其他 Chameleon 模板的 Chameleon 宏。

您可以尝试安装 five.pt,它会修补 Zope 以对所有页面模板使用 Chameleon。

关于plone - 在 Chameleon 中使用 tal 宏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22946663/

相关文章:

python - Chameleon i18n 和多元化

internationalization - 在 Pyramid 中使用国际化电子邮件模板的最佳方式

python - 使用 Chameleon ZPT 渲染具有任意深度的嵌套元素

python - 如何在plone的模板页面(html页面)中渲染matplotlib窗口

python - 如何从工作流转换的状态更改对象中获取审阅者姓名?

plone - 重命名 Plone "Members"id

full-text-search - Plone 中的自定义 SearchableText 和 HTML 字段

javascript - 按钮在 Plone 中不显示,onclick 也不起作用

python - Chameleon 中的 "while"循环?

python - Chameleon 和 Jinja2 之间的速度比较