首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用通用接口标记内容类型的更好方法

使用通用接口标记内容类型的更好方法
EN

Stack Overflow用户
提问于 2012-07-29 13:18:06
回答 2查看 114关注 0票数 5

我想要一个viewlet应用于同一个python蛋中的几个内容类型的视图。我一直在做的是通过browser/configure.zcml应用标记接口

代码语言:javascript
复制
<configure
    xmlns="http://namespaces.zope.org/zope"
    xmlns:browser="http://namespaces.zope.org/browser"
    i18n_domain="AnnualProgramModule.content">

  <include package="plone.app.contentmenu" />

  <class class="..content.programyear.ProgramYear">
    <implements interface=".viewlets.IAnnualProgram" />
  </class>

  <class class="..content.institution.Institution">
    <implements interface=".viewlets.IAnnualProgram" />
  </class>

</configure>

在我的基于Grok的模板中,我有:

代码语言:javascript
复制
from zope.interface import Interface
from five import grok
from plone.app.layout.viewlets.interfaces import IAboveContentTitle
from AnnualProgramModule.content.interfaces import IInstitution

grok.templatedir('templates')

class IAnnualProgram(Interface):
    """Marker Interface for AnnualProgram content types
    """

class AnnualProgramViewlet(grok.Viewlet):
    grok.require('zope2.View')
    grok.viewletmanager(IAboveContentTitle)
    grok.context(IAnnualProgram)

class InstitutionViewlet(grok.Viewlet):
    grok.require('zope2.View')
    grok.context(IInstitution)
    grok.viewletmanager(IAboveContentTitle)

这是可行的。但我很有兴趣知道是否有更好的方法。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-07-29 16:03:23

不,你所做的实际上是处理这个问题的最好方法。使用标记接口是我在任何情况下都会使用的方法。:-)

另一种方法是为所有不同内容类型的接口或类重新注册视图小程序:

代码语言:javascript
复制
<browser:viewlet
    name="yourpackage.annualprogram"
    for="..content.programyear.ProgramYear"
    manager="plone.app.layout.viewlets.interfaces.IAboveContentTitle"
    template="annualprogram.pt"
    permission="zope2.View"
    />

<browser:viewlet
    name="yourpackage.annualprogram"
    for="..content.interfaces.IInstitution"
    manager="plone.app.layout.viewlets.interfaces.IAboveContentTitle"
    template="annualprogram.pt"
    permission="zope2.View"
    />

但这要冗长得多。

票数 0
EN

Stack Overflow用户

发布于 2012-08-01 14:06:36

作为另一种选择,这也是可行的:

我添加了interface/annualProgram.py(我使用paster创建我的产品)。在它里面我有:

代码语言:javascript
复制
from zope.interface import Interface    

class IAnnualProgram(Interface):
"""A common marker interface for AnnualProgram ContentTypes"""

然后,在我的institution.py中,我添加了:

代码语言:javascript
复制
from AnnualProgramModule.content.interfaces import IAnnualProgram
.....

class Institution(folder.ATFolder):
    """Institution Profile"""
    implements(IInstitution, IAnnualProgram)

然后,我将相同的内容应用于需要IAnnualProgram的其他内容类型。

这是可行的,但并不一定更好。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11706877

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档