【问题标题】:Using tal macro in Chameleon在 Chameleon 中使用 tal 宏
【发布时间】:2014-04-11 04:17:53
【问题描述】:

我是 Plone 用户,我一直在使用 Products.Five.browser.pagetemplatefile.ViewPageTemplateFile 中的 tal 宏,并创建了一个现有宏和模板库。

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

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

一直在找兼容层,但目前发现的只有z3c.pt,目的是为了加快.pt页面的渲染速度,不提供兼容层。[2]

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

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

任何使用 chameleon 的客户端视图

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)

【问题讨论】:

    标签: plone chameleon


    【解决方案1】:

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

    您可以尝试安装 Five.pt,它会为 Zope 提供猴子补丁,以将 Chameleon 用于所有页面模板。

    【讨论】:

    • 我尝试使用 z3c.pt 使用 chameleon 解析器解析现有的 main_template.pt 页面,这样我就可以从中加载宏,但是我遇到了太多我不想处理的解析错误。作为一个临时解决方案,我使用旧模板创建了旧式 BrowserView 页面,我在其中插入了变色龙生成的 html 作为结构。
    猜你喜欢
    • 1970-01-01
    • 2011-12-20
    • 2013-11-27
    • 1970-01-01
    • 1970-01-01
    • 2012-06-16
    • 2016-04-29
    • 1970-01-01
    • 2015-10-30
    相关资源
    最近更新 更多