热搜: 活动 交友 discuz
查看: 3540|回复: 0


发表于 2012-11-11 19:24:24 | 显示全部楼层 |阅读模式
This will show you how to make a traversable multi-adapter on the context and request object that can be called within tal expressions and used as utilities elsewhere. It is done the exact same way as the plone_tools, plone_context_state, plone_portal_state BrowserViews you see used often throughout Plone viewlets and tal expressions.
  • First off, you'll have to add this in one of your own Plone products.  I will not go through creating a product in this tutorial.
  • Next, create a few files. Remember you can customize these to your liking.  I will call this BrowserView "tool."
  • In the file do something like this,
    1. from zope.interface import implements
    2. from zope.component import getMultiAdapter
    3. from plone.memoize.view import memoize, memoize_contextless

    4. from Acquisition import aq_inner
    5. from Products.CMFCore.utils import getToolByName
    6. from Products.Five.browser import BrowserView

    7. from interfaces import ITool

    8. class Tool(BrowserView):

    9.   implements(IUWOshTools)

    10.   @memoize_contextless
    11.   def hello_world(self):
    12.     return "Hello, World!"

    • Now in do something like this,
      1. from zope.interface import Interface

      2. class ITool(Interface):
      3.   def hello_world(self):
      4.     pass

      • In the file you'll need to declare each method you'll create in the Tool BrowserView
      • Now add this to your configure.zcml
        1. <browser:page
        2.   name="tool"
        3.   for="*"
        4.   permission="zope2.View"
        5.   class=".tool.Tool"
        6.   allowed_interface=".interfaces.ITool"
        7.   />

        8. <!-- We need to make the request annotatable for this to work -->
        9.   <class class="ZPublisher.BaseRequest.BaseRequest">
        10.     <implements interface="zope.annotation.interfaces.IAttributeAnnotatable" />
        11.   </class>

        • Now in a tal expression you can do things like this,
          1. here/@@tool/hello_world
          2. python: here.restrictedTraverse("@@tool").hello_world()

          • Or in code you can do something like this,
            1. from zope.component import getMultiAdapter

            2. tool = getMultiAdapter((self.context, self.request), name=u'tool')


使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册


QQ|小黑屋|Archiver|手机版|Plone技术资料 ( 湘ICP备14006519号-1 )

GMT+8, 2019-11-23 04:27 , Processed in 0.046969 second(s), 18 queries , Gzip On.

Powered by Plone! X3.4

© 2001-2019

快速回复 返回顶部 返回列表