如何从TAL Portlet调用文件系统中的方法(由collective.portlet.tal
提供)?
我就这样做了:我在我的案例中定义了一个新的BrowserView(createPictMenu.py
),然后将其注册为新portlet组件的渲染器:
class AddressTAL(BrowserView)
def my_address()
address_bar = ViewTemplatePageFile('templates/address_left.pt') # this
is the page template I want for my new portlet.
在configure.zcml
:
<plone:portlet
name="collective.portlet.tal.TALPortlet"
interface="collective.portlet.tal.talportlet.ITALPortlet"
assignment="collective.portlet.tal.talportlet.Assignment"
view_permission="zope2.View"
edit_permission="cmf.ManagePortal"
renderer=".browser.createPictMenu.AddressTAL"
addview="collective.portlet.tal.talportlet.AddForm"
editview="collective.portlet.tal.talportlet.EditForm"
/>
然后我转到localhost:8080 / myproject / @@ manage-portlets并从Add Portlet下拉列表中选择TAL Portlet option
。我将标题称为地址,并在说明中插入了以下代码段以致电address_tal()
:
<span tal:define="global li view/myaddress">
<span tal:replace="structure li" />
</span>
不幸的是,它没有用。请帮忙。
答案 0 :(得分:0)
该方法在文件系统上的信息或内容非常少。这应该是它应该的地方。拥有它在ZODB中是可能的但是一个坏主意。
但是有很多类型的方法,你从TAL调用它们的方式不同。
您可以在ZMI中使用通过portal_skins访问的“脚本(Python)方法”,您可以在内容对象上使用方法,并且可以在视图上使用方法。这些都可以从TAL调用。
对于既不是这些方法的方法,您必须创建一个可以调用的上述类型的方法,然后调用您要调用的方法。对于portlet,创建该方法的显而易见的地方是在Renderer上添加一个方法,这是一种视图,您可以从portlet模板调用它。
在您的情况下,您想要调用的方法已经是渲染器上的方法。这意味着你只需要打电话。
<p tal:content="view/myaddress" />
请注意,您已忘记定义中的self
参数。另外,请关注PEP8。
答案 1 :(得分:0)
您应该使用browser:view指令注册您的类AddressTAL,而不是plone:portlet one。像这样:
<configure
xmlns="http://namespaces.zope.org/zope"
xmlns:browser="http://namespaces.zope.org/browser">
<browser:page
name="address-view"
class=".browser.createPictMenu.AddressTAL"
for="*"
permission="zope2.View"
/>
</configure>
然后用:
来调用它<div tal:define="my_address python:context.restrictedTraverse('@@address-view').my_address()" >
Your method returns <span tal:content="my_address" />
</div>
或者:
<div tal:define="address_view context/@@address-view" >
Your method returns <span tal:content="address_view/my_address" />
</div>