簡體   English   中英

自定義遍歷和頁面模板

[英]Custom traversal and page templates

使用Marius Gedminas的出色博客文章 ,我為網站中的文件夾創建了一個自定義遍歷器。

這使我可以顯示: http://foo.com/folder/random_id : http://foo.com/folder/random_id

而不是: http://foo.com/folder/object.html?id=random_id : http://foo.com/folder/object.html?id=random_id

配置方面很好用,我可以捕獲random_ids並在消息中搜索正確的消息,以供顯示。

我的問題是我不確定如何通過慣用的頁面模板顯示數據-在他原始代碼的TODO點;)

if name == 'mycalendar':
            mycalendar = ... # TODO: do something to get the appropriate object
            return mycalendar

通常我會用類似的東西:

class Test(BrowserPage):

    template = ViewPageTemplateFile('atest.pt')

    def __call__(self):
        return self.template()

但是我無法解決如何在自定義遍歷的上下文中正確執行此操作。


更新 :明確地說,我想避免在URL中添加其他任何內容( http : //foo.com/folder/random_id/read )。

不需要通過任何其他地址查看該視圖( http : //foo.com/folder/read

我要使用的視圖的ZCML是:

<browser:page
  for="foo.interfaces.IFooFolderContainer"
  name="read"
  template="read.pt"
  permission="zope.ManageContent"
/>

我猜(根據進一步的建議),大致類似於:

return getMultiAdapter((mycalendar, self.request), IPageTemplate, name=u'read')

甚至返回要返回的對象類型的默認視圖(在這種情況下為字典):

<browser:page
  for="dict"
  name="read"
  template="read.pt"
  permission="zope.ManageContent"
/>

如果您顯示了自定義遍歷程序正在做什么,將更容易回答您的問題。

本質上,您想要這樣的東西:

def publishTraverse(self, request, name):
    if name in self.context:
        return MyMessageView(self.context[name], request)

    # fall back to views such as index.html
    view = queryMultiAdapter((self.context, request), name=name)
    if view is not None:
        return view

    # give up and return a 404 Not Found error page
    raise NotFound(self.context, name, request)

MyMessageView可以像

class MyMessageView(BrowserPage):
    __call__ = ViewPageTemplateFile('read.pt')

免責聲明:我不確定您直接實例化的視圖是否會受到安全包裝的保護; 確保您的功能測試確保匿名用戶無法查看郵件(如果您要這樣做的話)。

如果您使用自定義遍歷程序最終找到了合適的對象,則只需添加模板名稱和該模板中的用戶“上下文”即可。 因此, http://foo.com/folder/random_id/my_template並在模板中執行常規的<h1 tal:content="context/title" />東西。

IIUC,您想要的是在有人請求/ folder / random_id時呈現“讀取”視圖。 在這種情況下,您所需要做的就是使遍歷返回一個表示random_id的對象(可能是IFolderContent),並將“視圖”頁面指定為IFolderContent的defaultView。

需要defaultView,因為您的URL中沒有為random_id對象指定視圖。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM